简体   繁体   English

在ios的Safari浏览器中,当有一个“ onclick”内联孩子时,有一个“ onmouseover”内联父母时,为什么单击该孩子时触发了mouseover事件?

[英]In safari of ios, when there is a “onclick” inline a child, and a “onmouseover” inline a parent, why mouseover event fired when click the child?

In safari of ios, when there is a "onclick" inline a child element, and a "onmouseover" inline a parent element. 在safari的Safari中,当有一个内联子元素的“ onclick”和一个内联父元素的“ onmouseover”时。 When touch the child, I want the click event be fired, but first the mouseover event be fired. 当触摸孩子时,我希望触发click事件,但首先触发mouseover事件。

<span onmouseover='dosomething1()'>
    <ul>
        <li onclick='dosomething2()'></li>
    </ul>
</span>

I open this html in safari in iPhone, when I click/touch the li element, dosomething1 executed, and I click/touch the li element again, dosomething2 executed. 我在iPhone的野生动物园中打开此html,当我单击/触摸li元素时,执行dosomething1,然后再次单击/触摸li元素,执行dosomething2。 What can I do to make sure only dosomething2 execute when I click the li element? 当我单击li元素时,如何确保只执行dosomething2?

Because the mouseover event bubbles from the child to the parent, and for maximum compatibility, mobile browsers fire a mouseover event when the finger position changes. 由于mouseover事件从孩子到家长,并实现最大的兼容性气泡 ,移动浏览器触发一个mouseover事件时手指位置的变化。

If you use proper event handling, or pass the event object into dosomething1 , dosomething1 can look at event.target to see whether the target of the mousemove was the span or a descendant element and take appropriate action (or appropriately take no action). 如果使用适当的事件处理或将event对象传递到dosomething1 ,则dosomething1可以查看event.target来查看mousemove的目标是span还是其后代元素,并采取适当的操作(或适当地不采取任何操作)。

Alternately, you can hook mouseover on (say) the ul and stop the event from bubbling ("propagating") to its ancestors. 或者,您可以将mouseoverul并阻止事件冒泡(“传播”)到其祖先。

Example of using proper event handling and doing the check in the mousemove handler: 使用适当的事件处理并在mousemove处理程序中进行检查的示例:

 document.getElementById("the-span").addEventListener("mousemove", function(e) { if (e.target === this) { dosomething1(); } }); document.getElementById("the-li").addEventListener("click", dosomething2); function dosomething1() { console.log("mousemove on the span"); } function dosomething2() { console.log("click on the li"); } 
 <span id="the-span"> <ul> <li id="the-li">Text</li> </ul> text in span outside the li </span> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM