- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我收到有关按钮单击和背景单击的事件。在后台单击中,我得到的 currentTarget 和 Target 都相同,但在按钮的情况下,我得到的不同。如何比较它们是否相等,以便在相等时我可以执行某些操作?
最佳答案
Event Object具有一些属性,其中 3 个是最重要的:
Event.target - 引用“事件起源”。通俗地说:点击的按钮、用户输入文本的文本框、用户选择的单选按钮等。
Event.currentTarget - 对已注册事件监听器/处理程序的引用。如果 currentTarget 恰好是其他标签的祖先 - 那么 currentTarget 也可以监听所有这些标签的事件。这是可能的,因为事件传播/冒泡,请参阅Event Delegation更多细节。
Event.type - 基本上是点击、输入、更改、提交、重置、DOMContentLoaded 等
The key to Event Delegation is to find out if
Event.target
(which changes) is notEvent.currentTarget
(which doesn't change unless it's unregistered to event(s).).
演示中评论了详细信息
// Reference <form>
const current = document.forms[0];
// Register <form> to click, input, and change events
current.onclick = current.oninput = current.onchange = manageEvt;
// Callback function passes Event Object (e)
function manageEvt(e) {
// This is <form> - tag registered to event(s)
const cur = e.currentTarget;
// This is the tag that is event origin (clicked, changed, etc.)
const tgt = e.target;
// This is the event type (click, input, and change)
const evt = e.type;
/*
"this" is currentTarget (<form>)
.elements is all form controls of "this"
.out is the <output> id
*/
const out = this.elements.out;
// Clear <output>
out.value = '';
/*
if clicked, inputted, or changed tag is NOT <form>...
...display message...
Otherwise if only the <form> is clicked display another message.
*/
if (tgt !== cur) {
out.value = '#' + tgt.id + ' is Event.target for ' + evt.toUpperCase() + ' EVENT ------------------value: ' + tgt.value;
}
if (tgt === cur) {
out.value = '#' + tgt.id + ' is Event.target and Event.currentTarget for ' + evt.toUpperCase() + ' EVENT';
}
}
form {
border: 3px dashed red;
padding: 30px;
}
<form id='CURRENT'>
<button id='BUTTON' name='tgt' type='button' value="button">CLICK EVENT</button><br>
<input id='TEXT' name='tgt' type='text' placeholder='INPUT EVENT'><br>
<input id='RADIOA' name='tgt' type='radio' value="A"> A
<input id='RADIOB' name='tgt' type='radio' value="B"> B<br>
<output id='out'></output>
</form>
关于javascript - 如何比较 e.currentTarget 和 e.Target?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55466966/
这个问题在这里已经有了答案: Get a CSS value with JavaScript (8 个答案) 关闭 1 年前。 currentTarget 在我添加 style html 元素时起作
这个问题在这里已经有了答案: Get a CSS value with JavaScript (8 个答案) 关闭 1 年前。 currentTarget 在我添加 style html 元素时起作
我在学习 event.target和 event.currentTarget .我想我很清楚两者之间的区别。但陷入了event.currentTarget值原来是空的。 以下是 HTML 和 JS 代
为什么e.currentTarget.id显示警告? void clickHandler(Event e){ var label = e.currentTarget.id; 最佳答案 必须将e
我有一个组件,我可以使用它来保持单击,以便通过按住来调用多个功能。我的操作向 Redux reducer 分派(dispatch)一个简单的函数。 我的组件的目标是让人们通过保持鼠标点击来减少订单数量
因此,我尝试创建一个可重用的函数,供我页面上的每个 .featured-image 使用。如果我不使用 Backbone 事件:并且我只编写注释掉的代码,它就可以工作。我如何获得事件 imageOve
我在 Tap 上有 knockout 绑定(bind) 这应该获得目标 attr 数据 anchor ,该 anchor 适用于桌面但不适用于 Android.. scrollTo() {
我需要一些关于 JavaScript 的帮助,我需要 currentTarget 的替代方法,因为我的 JavaScript 程序不能在 Internet Explorer 8 上运行,所以我需要任何
event.currentTarget 和 this 有区别吗?性能怎么样? 最佳答案 currentTarget 事件属性返回事件监听器触发事件的元素。这仅在捕获和冒泡期间特别有用。 您也可以使
我用这个代码 document.addEventListener('click', (event) => { const { className, id, src = null, href =
下面两种方法有什么区别吗? #1 比 #2 快吗? #1 $('#selector').on('click',function(){ $(this)... // do stuff with c
请看下面的代码。 function deferredClick(f) { return (function (e) { var $this = $(e.currentTarge
我有一个事件处理程序,我将其附加到一个元素以捕获冒泡的点击事件。我需要一种可靠方法来获取捕获事件的元素,而不是触发它的元素。此特定元素高于 srcElement/target 的级别数量是任意的,因此
我的 Javascript 与 IE8 不兼容,我不确定如何解决它。我收到一个错误“currentTarget 为 null 或不是一个对象”,它出现在这段代码中: (function() { v
我有我的 html 的这一部分(不止一个相同类型): iPhone 345445 我的这部分 javascript 代码旨在获取 span 标签的 innerHTML 并为它们分配值,如下
我构建了这个小示例。我有一些元素,其中一些有 15% 的折扣。我想在按下“购买”时将此值(价格的 15%)推送到数组中,并且仅当按下的商品有折扣时。 我的想法是使用 currentTarget 来识别
在每个 jQuery 事件中,jQuery 都提供与 this 相同的 currentTarget,但据我了解 currentTarget 的属性取决于在您的浏览器上。 在 chrome 中,我可以获
考虑: let sel=document.getElementById('mys'); sel.onchange=function(e) { console.log(e.currentTarget
我正在尝试制作一个简单的应用程序,其中有 1 个全局事件监听器监听点击事件。 此时,每当我单击一个按钮时,即使其中有文本,它也会返回 undefined(按钮开始时没有文本,但随后会被填充)。 如何获
我有一个外部 div 捕获 onWheel鼠标事件。在处理程序中,我想知道鼠标指针相对于外部 div 的坐标。例如: constructor() { this.handleWheel = this
我是一名优秀的程序员,十分优秀!