javascript - 關于mouseenter的問題
問題描述
<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://www.piao2010.com/wenda/jquery/jquery-3.2.1.js'></script></head><body> <p>只有在鼠標指針穿過被選元素時,才會觸發(fā) mouseenter 事件。</p> <p class='enter'><h2 >被觸發(fā)的 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>
當我用絕對定位把子元素移到下面,這時穿過子元素也會觸發(fā)事件,這是怎么回事?
問題解答
回答1:absolute positioning 只是將元素抽離了 normal flow ,并沒有改變 document tree 的結構,所以子元素依然算是在父元素里面。
解決方法可以是判斷 event.target 是不是子元素,或者改為給兩者綁定 mouseover 然后在子元素里 stopPropagation 。
回答2:根據(jù)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.
翻譯一下就是:
當指針一類的東西移到某個元素的邊界上,或者它的某個后代元素的邊界上,就必須觸發(fā)mouseenter事件。而當指針從某個元素里,移到它的某個后代元素的邊界上時,則不可觸發(fā)mouseenter事件。
所以對于你的問題,回答就是,移到后代上也會觸發(fā)mouseenter是人家規(guī)定了的
相關文章:
1. python - scrapy 如何組合2個不同頁面的數(shù)據(jù),一并存儲2. mysql優(yōu)化 - mysql 一張表如果不能確保字段列長度一致,是不是就不需要用到char。3. node.js - mysql如何通過knex查詢今天和七天內(nèi)的匯總數(shù)據(jù)4. javascript - 用jsonp抓取qq音樂總是說回調(diào)函數(shù)沒有定義5. javascript - 新浪微博網(wǎng)頁版的字數(shù)限制是怎么做的6. sublime可以用其他編譯器替換嗎?7. python2.7 - python 函數(shù)或者類 代碼的執(zhí)行順序8. 使用python中的pandas求每個值占該列的比例9. python - 多態(tài)調(diào)用方法時卻顯示bound method...10. mysql 怎么做到update只更新一行數(shù)據(jù)?
