2016-07-28 54 views
3

当你点击“href”链接时,我有这个jQuery使FadeOut平滑页面。我该怎么办jQuery不适用于特定元素?

我有一个电子商务,所以我有一个购物车,你可以看到你添加的产品。 (在线商店:www.backlabel.com

您可以直接用产品顶部的“X”将其从购物车中删除。这个“X”具有“href”属性,所以页面加载jQuery和它的坏,因为整个页面重新加载。

我希望jQuery不能在“X”按钮上工作。我可以在此jQuery中使用额外的代码吗?

// delegate all clicks on "a" tag (links) 
$(document).on("click", "a", function (event) { 
    // get the href attribute 
    var newUrl = $(this).attr("href"); 

    // veryfy if the new url exists or is a hash 
    if (!newUrl || newUrl[0] === "#") { 
     // set that hash 
     location.hash = newUrl; 
     return; 
    } 

    // now, fadeout the html (whole page) 
    $("html").fadeOut(function() { 
     // when the animation is complete, set the new location 
     location = newUrl; 
    }); 

    // prevent the default browser behavior. 
    return false; 
}); 
+0

防止默认浏览器行为使用event.preventDefault() – atul

回答

5

可以排除href从这样的选择使用a:not('#yourID')

$(document).on("click", "a:not('#x')", function (event) { 
 
    alert('clicked'); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<a href="#" >Link1</a> 
 
<a href="#" >Link2</a> 
 
<a href="#" id="x">Link3</a>

1
$("html").fadeOut(function() { 
     // when the animation is complete, set the new location 
     window.location = newUrl; 
    });