jquery 令人困惑的代码
我正在从网络示例中学习 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
td[alt*="Collection"]
选择alt
属性包含Collection
的所有元素,例如:
但不是
旁注:这是一个非常基本的问题,可以通过阅读 jQuery 选择器轻松回答API文档:
请在提问之前先尝试研究一下!
td[alt*="Collection"]
selects all<td>
elements whosealt
attribute containsCollection
, such as:but not
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!
这是一个 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 namedalt
whose string contains the valueCollection
.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
该代码返回“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 :)
可以像这样更简单和简短地重写此代码:
并且,这就是它的工作原理:
This code can be rewritten more simply and briefly like this:
And, this is how it works: