亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

請問mouseenter不是不支持冒泡嗎,為什么$("input").trigger("mouseenter")可以觸發事件呢?

<!DOCTYPE?html>
<html>

<head>
????<meta?http-equiv="Content-type"?content="text/html;?charset=utf-8"?/>
????<title></title>
????<style>
????????.left?div,
????????.right?div?{
????????????width:?500px;
????????????height:?50px;
????????????padding:?5px;
????????????margin:?5px;
????????????float:?left;
????????????border:?1px?solid?#ccc;
????????}

????????.left?div?{
????????????background:?#bbffaa;
????????}

????????.right?div?{
????????????background:?yellow;
????????}
????</style>
????<script?src="https://cdn.bootcss.com/jquery/1.10.2/jquery.min.js">
????</script>
</head>

<body>
<h2>自定義事件triggerHandler</h2>
<div?class="left">
????<div?id="accident">
????????<a>triggerHandler事件</a>
????????<form?name="input"?action=""?method="get">
??????????<input?type="text"?value="momomo">
????????</form>
????</div>
????<button>事件冒泡,觸發瀏覽器默認聚焦行為</button><br><br>
????<button>不會冒泡,不觸發瀏覽器默認聚焦行為</button>
</div>
<script?type="text/javascript">

????//給input綁定一個聚焦事件
????var?n=0;
????$("#accident").on("mouseenter",function(event,title)?{
????????title=title||"默認";
????????$("input").val(++n);
????});

????$("#accident").on("click",function(e)?{
????????alert("trigger觸發的事件會在?DOM?樹中向上冒泡");
????});
????//trigger觸發focus
????$("button:first").click(function()?{
????????$("a").trigger("click");
????????$("input").trigger("mouseenter","傳遞");
????});

????//triggerHandler觸發focus
????$("button:last").click(function()?{
????????$("a").triggerHandler("click");
????????$("input").triggerHandler("mouseenter","沒有觸發默認聚焦事件");
????});



</script>
</body>

</html>


正在回答

1 回答

在jquery的trigger源碼中:

ontype = type.indexOf( ":" ) < 0 && "on" + type;

.....

// Fire handlers on the event path

i = 0;

while ( ( cur = eventPath[ i++ ] ) && !event.isPropagationStopped() ) {

????lastElement = cur;?

????event.type = i > 1 ?bubbleType :special.bindType || type;

????// jQuery handler

????handle = ( dataPriv.get( cur, "events" ) || {} )[ event.type ] &&dataPriv.get( cur, "handle" );

????if ( handle ) {

????????handle.apply( cur, data );

????}

????// Native handler

????handle = ontype && cur[ ontype ];

????if ( handle && handle.apply && acceptData( cur ) ) {

????????event.result = handle.apply( cur, data );

????if ( event.result === false ) {

????????event.preventDefault();

????}

}

type 是事件類型。

eventPath就是從target 到window對象 的一條路徑。

當event.isPropagationStopped() 為假時,會遍歷eventPath,調用每個符合的jquery處理器。


從源碼上知道, type='mouseenter' ?ontype = 'onmouseenter'。必然分別調用Jquery,原生接口中的處理函數。

在原生接口中,對應event[ontype]

jquery接口中,對應dataPri.get(cur,'events')[type] ?或者dataPriv.get(cur,'handle') 。

而elem.on() 函數的調用:elem.on() -> jquery.on() -> event.add() 最終添加到隊列中。




0 回復 有任何疑惑可以回復我~
#1

DeppSparrow 提問者

非常感謝!
2017-12-29 回復 有任何疑惑可以回復我~
#2

tobeyous

看不懂
2018-02-22 回復 有任何疑惑可以回復我~
#3

qq_演繹陌路離傷_3 回復 tobeyous

求也看不懂,你說說你會啥
2018-05-03 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消

請問mouseenter不是不支持冒泡嗎,為什么$("input").trigger("mouseenter")可以觸發事件呢?

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號