問題描述

創(chuàng)新互聯(lián)主營安岳網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,重慶App定制開發(fā),安岳h5小程序定制開發(fā)搭建,安岳網(wǎng)站營銷推廣歡迎安岳等地區(qū)企業(yè)咨詢
最近在使用jquery模擬a標簽的click事件,無法觸發(fā)其默認行為。即click()或trigger('click')無法觸發(fā)href跳轉(zhuǎn)。
<a id="aBtn" >去卡咪官網(wǎng)</a>
$('#aBtn').click();//無法跳轉(zhuǎn),不生效
$('$aBtn').trigger('click');//同樣無法跳轉(zhuǎn),不生效下面就來一起分析下其原因與解決方法吧
問題原因
jquery內(nèi)部實現(xiàn)click或trigger方法時,并未真正模擬用戶點擊事件,只是模擬了事件對象及冒泡的觸發(fā)。(最后附有jquery實現(xiàn)源碼供參考)
解決方案
解決思路:在原生dom觸發(fā)click事件或利用事件冒泡來解決。
原生dom觸發(fā)click
<a id="aBtn" >去卡咪官網(wǎng)</a>
document.querySelector('#aBtn').click();//原生dom觸發(fā) 或者
$('#aBtn')[0].click();//jquery對象轉(zhuǎn)為dom對象再觸發(fā)利用子元素事件冒泡
<a id="aBtn" >
<span id="spanBtn">去卡咪官網(wǎng)</span>
</a>
$('#spanBtn').click();//或者
$('#spanBtn').trigger('click');jquery trigger()實現(xiàn)源碼(8159行-8304行)
源碼鏈接地址
關(guān)鍵摘要:
// Fire handlers on the event path (8237行)
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 ) {
//******自身trigger('click')或click()時,會調(diào)用緩存列表里的事件回調(diào)函數(shù),但未執(zhí)行elem.click()******
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();
}
}
}// If nobody prevented the default action, do it now (8263行)
if ( !onlyHandlers && !event.isDefaultPrevented() ) {
if ( ( !special._default ||
special._default.apply( eventPath.pop(), data ) === false ) &&
acceptData( elem ) ) {
// Call a native DOM method on the target with the same name as the event.
// Don't do default actions on window, that's where global variables be (#6170)
if ( ontype && isFunction( elem[ type ] ) && !isWindow( elem ) ) {
// Don't re-trigger an onFOO event when we call its FOO() method
tmp = elem[ ontype ];
if ( tmp ) {
elem[ ontype ] = null;
}
// Prevent re-triggering of the same event, since we already bubbled it above
jQuery.event.triggered = type;
if ( event.isPropagationStopped() ) {
lastElement.addEventListener( type, stopPropagationCallback );
}
//******子元素trigger('click')或click(),會執(zhí)行elem.click()******
elem[ type ]();
if ( event.isPropagationStopped() ) {
lastElement.removeEventListener( type, stopPropagationCallback );
}
jQuery.event.triggered = undefined;
if ( tmp ) {
elem[ ontype ] = tmp;
}
}
}
}總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,如果有疑問大家可以留言交流,謝謝大家對創(chuàng)新互聯(lián)的支持。
網(wǎng)頁題目:使用jquery模擬a標簽的click事件無法實現(xiàn)跳轉(zhuǎn)的解決
本文網(wǎng)址:http://chinadenli.net/article0/jgjhoo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站營銷、營銷型網(wǎng)站建設(shè)、網(wǎng)站制作、品牌網(wǎng)站制作、全網(wǎng)營銷推廣、定制開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)