开发者

Understand JQuery Snippet code [closed]

开发者 https://www.devze.com 2023-03-20 23:51 出处:网络
It&#开发者_开发问答39;s difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form.
It&#开发者_开发问答39;s difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

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:

  1. Finding the next immediate sibling of the link that was clicked.
  2. Then finding every element with class addtocart that's a child of that sibling element,
  3. 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消