久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術文章
文章詳情頁

javascript - 關于mouseenter的問題

瀏覽:114日期:2023-02-28 10:27:12

問題描述

<head><style> .enter h2{border:1px solid;background: white;position: absolute;top: 200px; } .enter{ border:1px solid; background: #eee; width: 500px; height: 100px; }</style><script type='text/javascript' src='http://m.baoyu77737.com/wenda/jquery/jquery-3.2.1.js'></script></head><body> <p>只有在鼠標指針穿過被選元素時,才會觸發 mouseenter 事件。</p> <p class='enter'><h2 >被觸發的 Mouseenter 事件:<span></span></h2> </p><script type='text/javascript'> x=0; y=0; $(document).ready(function(){ $('p.enter').mouseenter(function(){$('.enter span').text(y+=1); }); });</script></body>

javascript - 關于mouseenter的問題

當我用絕對定位把子元素移到下面,這時穿過子元素也會觸發事件,這是怎么回事?

問題解答

回答1:

absolute positioning 只是將元素抽離了 normal flow ,并沒有改變 document tree 的結構,所以子元素依然算是在父元素里面。

解決方法可以是判斷 event.target 是不是子元素,或者改為給兩者綁定 mouseover 然后在子元素里 stopPropagation 。

回答2:

根據https://www.w3.org/TR/uievent...

A user agent MUST dispatch this event when a pointing device is moved onto the boundaries of an element or one of its descendent elements. This event type is similar to mouseover, but differs in that it does not bubble, and MUST NOT be dispatched when the pointer device moves from an element onto the boundaries of one of its descendent elements.

翻譯一下就是:

當指針一類的東西移到某個元素的邊界上,或者它的某個后代元素的邊界上,就必須觸發mouseenter事件。而當指針從某個元素里,移到它的某個后代元素的邊界上時,則不可觸發mouseenter事件。

所以對于你的問題,回答就是,移到后代上也會觸發mouseenter是人家規定了的

標簽: JavaScript
相關文章:
主站蜘蛛池模板: 横峰县| 阿克苏市| 卫辉市| 新丰县| 澎湖县| 宜章县| 类乌齐县| 天津市| 手机| 高要市| 太仓市| 蒙自县| 新绛县| 荃湾区| 大冶市| 开远市| 和平区| 丰县| 招远市| 云南省| 加查县| 长岛县| 泰安市| 石门县| 梅州市| 博爱县| 怀集县| 荥阳市| 鹿邑县| 青浦区| 余姚市| 武强县| 鲜城| 延川县| 渝北区| 浦县| 高雄县| 广州市| 榕江县| 德兴市| 阿合奇县|