jquery 令人困惑的代码

发布于 2024-12-06 12:57:32 字数 542 浏览 2 评论 0原文

我正在从网络示例中学习 Jquery 和 Javascript。我有很好的工作知识,但有些代码仍然让我困惑。以下代码用于购物车,以隐藏结帐按钮并替换为显示有关最低购物车要求的消息的 div。不过,有一部分代码让我失望。

function getCollectionCount() {
  var totalCollectionCount = 0;    
  var collection = $('td[alt*="Collection"]');
  for (var i = 0; i < collection.length; i++) {
    var curVal = $(collection[i]).find("select").val();
    if (curVal != undefined){
      totalCollectionCount += parseInt(curVal);
    }
  }

这部分的意思是什么?

var collection = $('td[alt*="Collection"]');

I am learning Jquery and Javascript from web examples. I have a good working knowledge but some code still trips me up. The following code is used for a shopping cart to hide the check out button and replace with a div displaying a message about minimum cart requirements. There is a part of the code throwing me off though.

function getCollectionCount() {
  var totalCollectionCount = 0;    
  var collection = $('td[alt*="Collection"]');
  for (var i = 0; i < collection.length; i++) {
    var curVal = $(collection[i]).find("select").val();
    if (curVal != undefined){
      totalCollectionCount += parseInt(curVal);
    }
  }

What does this part mean?

var collection = $('td[alt*="Collection"]');

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

放我走吧 2024-12-13 12:57:32

td[alt*="Collection"] 选择 alt 属性包含 Collection 的所有 元素,例如:

<td alt="Collection"></td>
<td alt="CollectionFoo"></td>
<td alt="BarCollection12324343"></td>

但不是

<td></td>
<td alt=""></td>
<td alt="Foo"></td>

旁注:这是一个非常基本的问题,可以通过阅读 jQuery 选择器轻松回答API文档

请在提问之前先尝试研究一下!

td[alt*="Collection"] selects all <td> elements whose alt attribute contains Collection, such as:

<td alt="Collection"></td>
<td alt="CollectionFoo"></td>
<td alt="BarCollection12324343"></td>

but not

<td></td>
<td alt=""></td>
<td alt="Foo"></td>

Side note: this is a pretty basic question that could easily be answered by read the jQuery selectors API documentation:

Please do try to research before you ask!

寄人书 2024-12-13 12:57:32

这是一个 jQuery 属性选择器子句。它选择任何具有名为 alt 且字符串包含值 Collection 的属性的 td 元素。

包含选择器: http://api.jquery.com/attribute-contains-selector/

jQuery 有许多有用的属性选择器。这是他们的主要参考页面。如果您刚刚开始使用 jQuery,那么非常值得一读

This is a jQuery attribute selector clause. It's selecting any td element which has an atrtibute named alt whose string contains the value Collection.

Contains Selector: http://api.jquery.com/attribute-contains-selector/

jQuery has a number of useful attribute selectors. Here is the main reference page for them. Very much worth the read if you're just getting started with jQuery

守望孤独 2024-12-13 12:57:32

该代码返回“alt”属性包含“Collection”的每个 td 元素。

http://api.jquery.com/attribute-contains-selector/

jQuery 是充满了这些需要永远学习的时髦快捷方式,所以我总是保留一份jQuery in action任何时候我的办公桌:)

That code returns every td element whose "alt" attribute contains "Collection".

http://api.jquery.com/attribute-contains-selector/

jQuery is full of these funky shortcuts that take forever to learn, so I always keep a copy of jQuery in action on my desk at all times :)

缱绻入梦 2024-12-13 12:57:32

可以像这样更简单和简短地重写此代码:

function getCollectionCount() {
    var totalCollectionCount = 0;    
    $('td[alt*="Collection"] select').each(function() {
        var val = this.value || "0";
        totalCollectionCount += parseInt(val, 10);
    });
    return(totalCollectionCount);
}

并且,这就是它的工作原理:

  1. 将totalCollectionCount初始化为0
  2. 查找所有在alt属性中具有字符串“Collection”的td元素,然后查找该td中的选择元素
  3. 迭代所有 元素找到与上述条件匹配的元素
  4. 使用选择对象的值初始化局部变量,如果没有值或为空,则使用“0”
  5. 将该值转换为数字(必须将基数传递给 parseInt所以它不会猜测)并将其添加到到目前为止的小计中。
  6. 返回我们找到的总数。

This code can be rewritten more simply and briefly like this:

function getCollectionCount() {
    var totalCollectionCount = 0;    
    $('td[alt*="Collection"] select').each(function() {
        var val = this.value || "0";
        totalCollectionCount += parseInt(val, 10);
    });
    return(totalCollectionCount);
}

And, this is how it works:

  1. Initialize totalCollectionCount to 0
  2. Find all td elements that have the string "Collection" in the alt attribute and then find select elements within that td
  3. Iterate over all elements found that match the above condition
  4. Initialize a local variable with either the value of the select object or "0" if there is no value or it's empty
  5. Turn that value into a number (must pass the radix to parseInt so it won't guess) and add it to the sub-total so far.
  6. return the total we found.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文