大网数据广告活动
Web 标准与前端开发

HTMLElement: interest event

· 52CE 我爱测

The **`interest`** event of the HTMLElement interface is fired on an interest invoker's target element when interest is shown, allowing code to be run in response.

HTML DOM

The interest event of the HTMLElement interface is fired on an interest invoker's target element when interest is shown, allowing code to be run in response.

Syntax

Use the event name in EventTarget/addEventListener:

addEventListener("interest", (event) => { })

Alternatively, assign to the event handler property of the interface:

oninterest = (event) => { }

Event type

An InterestEvent. Inherits from Event.

InterestEvent

Examples

Basic interest invoker event usage

In this example, we use the interest and loseinterest events to report when interest is shown and lost on a button element that acts as an interest invoker. We do this by printing messages into the target p element's text content.

HTML

We set up the relationship between the <button> element interest invoker and its target <p> element by setting the value of the <button> element's interestfor attribute equal to the <p> element's id.

<button href="#" interestfor="mytarget">Interest invoker</button>
<p id="mytarget">No interest being shown currently.</p>

JavaScript

We get a reference to the <button> element and its target element via the HTMLButtonElement.interestForElement property.

const invoker = document.querySelector("[interestfor]");
const target = invoker.interestForElement;

We then set two event listeners on the target element, for the interest and loseinterest events.

  • When interest is shown, we update the target <p> element's text content to report the event and include the element that triggered it; in this example, that's the <button> element. Note how you can get a reference to the interest invoker via the event object's InterestEvent.source property.
  • When interest is lost, we update the paragraph text to report that interest is no longer being shown.
target.addEventListener("interest", (e) => {
  target.textContent = `Interest shown via the ${e.source.tagName} element.`;
});

target.addEventListener("loseinterest", () => {
  target.textContent = `Interest lost.`;
});

Result

The example renders like this:

basic-interest-invoker

Try showing and losing interest in the button (for example, by hovering or focusing it) to see how the <p> text changes.

Specifications

Browser compatibility

See also

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。