Can anyone help me understand what this code snippet is doing? I'm maintaining a website and I think this is the source of a problem I am having.
function cust_addToCart(itemid, itemqty, options, viaajx, loadingf, callback) {
var url = "/app/site/backend/additemtocart.nl?buyid=" + itemid + "&qty=" + itemqty;
document.location.href = url;
}
$('#itemlist .addtocart-lnk').click(function() {
$(this).next().find('.addtocart').click();
return false; //Would this return a # for a link?
});
This snippet:
$('#itemlist .addtocart-lnk').click(function() {
$(this).next().find('.addtocart').click();
return false; //Would this return a # for a link?
});
Is binding an event handler for the click
event on elements with a class addtocart-lnk
that are children of an element with id itemlist
. Inside that event handler, the following:
$(this).next().find('.addtocart').click();
Is:
- Finding the next immediate sibling of the link that was clicked.
- Then finding every element with class
addtocart
that's a child of that sibling element, - Programmatically triggering the
click
event on those elements
This line:
return false;
Is preventing the default behavior of the link (which is hard to say without seeing your markup). Usually this prevents a link from being followed and the browser showing a new page.
This function:
function cust_addToCart(itemid, itemqty, options, viaajx, loadingf, callback) {
var url = "/app/site/backend/additemtocart.nl?buyid=" + itemid + "&qty=" + itemqty;
document.location.href = url;
}
Is building a url by concatenating the first string with the arguments itemid
and itemqty
, which are passed into the function. Setting document.location.href
is directing the browser to that url.
精彩评论