2016-07-28 4 views
3

Ich habe diese jQuery zu FadeOut die Seite reibungslos, wenn Sie auf eine "href" -Link klicken.Wie kann ich tun jQuery funktioniert nicht für ein bestimmtes Element?

Ich habe einen E-Commerce, also habe ich einen Warenkorb, wo Sie die Produkte sehen können, die Sie hinzugefügt haben. (Online-Shop: www.backlabel.com)

Sie können es aus dem Warenkorb direkt mit einem "X" auf der Oberseite des Produkts löschen. Dieses "X" hat die Eigenschaft "href", also lädt die Seite die jQuery und es ist schlecht, weil die ganze Seite neu geladen wird.

Ich wünschte, jQuery funktioniert nicht nur auf die Schaltfläche "X". Kann ich es mit einem zusätzlichen Code in diesem jQuery folgen?

// 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

zur Verhinderung des Verhaltens Verwendung Event.preventDefault Standardbrowser() – atul

Antwort

5

Sie können eine href aus einer solchen Auswahl ausschließen Verwendung a:not('#yourID') wie die

$(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; 
    }); 
Verwandte Themen