簡體   English   中英

jQuery與帶有click()的另一個元素內的元素的jQuery不同click()函數

[英]jQuery different click() function for an element within another element with a click()

在我的網站上,我有一個側邊欄面板,其中有兩個選項卡,單擊這些選項卡可以切換下面的內容。 在這些標簽的<li> ,我有一個jQuery click()事件來切換標簽。 請查看屏幕截圖以了解我的意思

替代文字http://cci.epiphanydev2.co.uk/Home_1281538687319.png

一切正常,直到我們的設計師想要<li>的RSS feed鏈接,所以現在當我單擊RSS feed圖標時,它被<li>上的click()函數覆蓋。

到目前為止,我的jQuery選項卡如下:

//NEWS PANEL
$("div.switch-tab div.listing-box").hide();
$(".news ul.tabs li:first").addClass("active").show();
$("div.switch-tab div.listing-box:first").show();

$(".news ul.tabs li").click(function() {
    $(".news ul.tabs li").removeClass("active");
    $(this).addClass("active");
    $("div.switch-tab div.listing-box").hide();
    var activeTab = $(this).find("a").attr("href");
    $(activeTab).fadeIn();
    return false;
});
// ATTEMPT TO OVERRIDE
$(".news ul.tabs li a.feed").click(function() {
    return true;
});
//END NEWS PANEL

知道我如何解決這個問題嗎?

使用event.stopPropagation()

您正在尋找event.stopPropagation(): http : //api.jquery.com/event.stopPropagation/

您需要傳遞事件,然后將event.stopPropagation()放在供稿鏈接的click函數中。 所以做這樣的事情:

// ATTEMPT TO OVERRIDE
$(".news ul.tabs li a.feed").click(function(event) {
    event.stopPropagation();
    return true;
});

這將使click事件不再冒泡,直到包含父母,在您的情況下,選項卡<li>

在此處查看演示: http : //jsfiddle.net/kjdeG/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM