了解JQuery Snippet代码

-1 javascript jquery

任何人都可以帮我理解这段代码片段的作用吗?我正在维护一个网站,我认为这是我遇到的问题的根源.

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?
});
Run Code Online (Sandbox Code Playgroud)

And*_*ker 7

这个片段:

$('#itemlist .addtocart-lnk').click(function() {
    $(this).next().find('.addtocart').click();
    return false; //Would this return a # for a link?
});
Run Code Online (Sandbox Code Playgroud)

click在具有类的元素的元素上绑定事件的事件处理程序,该类addtocart-lnk是具有id的元素的子元素itemlist.在该事件处理程序中,以下内容:

$(this).next().find('.addtocart').click();
Run Code Online (Sandbox Code Playgroud)

方法是:

  1. 找到被点击的链接的下一个直接兄弟.
  2. 然后找到具有addtocart该兄弟元素的子类的每个元素,
  3. 以编程方式触发click这些元素的事件

这一行:

return false;
Run Code Online (Sandbox Code Playgroud)

是阻止链接的默认行为(如果没有看到你的标记很难说).通常,这可以防止链接被跟踪,浏览器显示新页面.


这个功能:

function cust_addToCart(itemid, itemqty, options, viaajx, loadingf, callback) {
    var url = "/app/site/backend/additemtocart.nl?buyid=" + itemid + "&qty=" + itemqty;
    document.location.href = url;
}
Run Code Online (Sandbox Code Playgroud)

通过将第一个字符串与参数连接起来构建一个url,itemid并将itemqty其传递给函数.设置document.location.href是将浏览器定向到该URL.