admin管理员组文章数量:1023794
I have been trying to get the target (<li>
element) from the mouseenter event but so far no luck!
The code:
<ul id="ul">
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
</ul>
<script>
var ul = document.getElementById('ul');
ul.onmouseenter = function(e) {
console.log(e.target);
};
</script>
Unfortunately the console keeps printing the <ul />
element. Any suggestions?
I have been trying to get the target (<li>
element) from the mouseenter event but so far no luck!
The code:
<ul id="ul">
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
</ul>
<script>
var ul = document.getElementById('ul');
ul.onmouseenter = function(e) {
console.log(e.target);
};
</script>
Unfortunately the console keeps printing the <ul />
element. Any suggestions?
2 Answers
Reset to default 6It's because the onmouseenter
event is not a "bubbling" event, so it only fires when you enter the UL
element, not when you enter nested elements, like the LI
elements.
Therefore the e.target
and the this
elements will both be the UL
.
If you use onmouseover
instead it'll bubble, so you'll get the LI
as the e.target
when entering the LI
elements.
You can get the targeted li like:
function getEventTarget(e) {
e = e || window.event;
return e.target || e.srcElement;
}
var ul = document.getElementById('ul');
ul.onclick= function(event) {
var target = getEventTarget(event);
console.log(target.innerHTML);
};
I have been trying to get the target (<li>
element) from the mouseenter event but so far no luck!
The code:
<ul id="ul">
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
</ul>
<script>
var ul = document.getElementById('ul');
ul.onmouseenter = function(e) {
console.log(e.target);
};
</script>
Unfortunately the console keeps printing the <ul />
element. Any suggestions?
I have been trying to get the target (<li>
element) from the mouseenter event but so far no luck!
The code:
<ul id="ul">
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
</ul>
<script>
var ul = document.getElementById('ul');
ul.onmouseenter = function(e) {
console.log(e.target);
};
</script>
Unfortunately the console keeps printing the <ul />
element. Any suggestions?
2 Answers
Reset to default 6It's because the onmouseenter
event is not a "bubbling" event, so it only fires when you enter the UL
element, not when you enter nested elements, like the LI
elements.
Therefore the e.target
and the this
elements will both be the UL
.
If you use onmouseover
instead it'll bubble, so you'll get the LI
as the e.target
when entering the LI
elements.
You can get the targeted li like:
function getEventTarget(e) {
e = e || window.event;
return e.target || e.srcElement;
}
var ul = document.getElementById('ul');
ul.onclick= function(event) {
var target = getEventTarget(event);
console.log(target.innerHTML);
};
本文标签: javascriptHow to get event target without jQueryStack Overflow
版权声明:本文标题:javascript - How to get event target without jQuery - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745600981a2158454.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论