从 jQuery 中的所有元素中获取属性值
Grab values of an attribute from all elements in jQuery
假设我们有以下 HTML:
<entries>
<entry id="1" />
<entry id="2" />
<entry id="3" />
<entry id="4" />
</entries>
jQuery 是否具有用于检索特定属性的所有值的内置机制?如果没有,那么检索它们的最有效方法是什么?
例如,类似于:$('entry').attrs('id')
,返回所有元素的值列表,returns类似于["1", "2", "3", "4"]
?
jQuery General Attributes 下的文档(这是找到 attr
的地方)没有给出任何关于存在这样的东西的提示,我在 Whosebug 上没有发现任何东西或提出此问题的任何其他支持论坛。
你可以使用类似的东西:https://jsfiddle.net/g903dyp6/
<entries>
<entry id="1" />
<entry id="2" />
<entry id="3" />
<entry id="4" />
</entries>
let arr = $.map($('entry'), function(el) {
return $(el).attr('id');
});
没有直接的功能可以做到这一点。但是,使用 .map() 可以轻松完成。例如,
let ids = $('entry').map(function() {
return this.getAttribute('id');
}).get();
let ids = $('entry').map(function() {
return this.getAttribute('id');
}).get();
console.log(ids);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<entries>
<entry id="1" />
<entry id="2" />
<entry id="3" />
<entry id="4" />
</entries>
您可以使用 jQuery.fn.extend()
扩展 jQuery 并添加一个自定义方法来遍历所有匹配元素,从所有元素中提取一个或多个属性并将它们 pushes/merges分别返回array/object。
由于实现是 vanilla JS,这将比在所有中间步骤中使用 jQuery 对象和函数更快,但可能需要根据您需要支持的浏览器进行一些调整:
jQuery.fn.extend({
attrs: function(attributeName) {
if (attributeName) {
return this.toArray().map((el) => el.getAttribute(attributeName));
}
return this.toArray().reduce((merged, el) => {
const { attributes } = el;
const totalAttributes = attributes.length;
for (let i = 0; i < totalAttributes; ++i) {
const attribute = attributes[i].nodeName;
if (merged[attribute]) {
merged[attribute].push(el.getAttribute(attribute));
} else {
merged[attribute] = [el.getAttribute(attribute)];
}
}
return merged;
}, {});
},
});
console.log($('entry').attrs());
console.log($('entry').attrs('id'));
console.log($('entry').attrs('class'));
.as-console-wrapper {
max-height: 100% !important;
}
<entries>
<entry id="1" class="foo" />
<entry id="2" class="bar" />
<entry id="3" class="baz" />
<entry id="4" class="qux" />
</entries>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
这是使用 JavaScript Array .map() 函数的一种方法:
let ids = jQuery.makeArray($('entry')).map(entry => entry.id);
console.log('ids:', ids);
<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js"></script>
<entries>
<entry id="1" />
<entry id="2" />
<entry id="3" />
<entry id="4" />
</entries>
假设我们有以下 HTML:
<entries>
<entry id="1" />
<entry id="2" />
<entry id="3" />
<entry id="4" />
</entries>
jQuery 是否具有用于检索特定属性的所有值的内置机制?如果没有,那么检索它们的最有效方法是什么?
例如,类似于:$('entry').attrs('id')
,返回所有元素的值列表,returns类似于["1", "2", "3", "4"]
?
jQuery General Attributes 下的文档(这是找到 attr
的地方)没有给出任何关于存在这样的东西的提示,我在 Whosebug 上没有发现任何东西或提出此问题的任何其他支持论坛。
你可以使用类似的东西:https://jsfiddle.net/g903dyp6/
<entries>
<entry id="1" />
<entry id="2" />
<entry id="3" />
<entry id="4" />
</entries>
let arr = $.map($('entry'), function(el) {
return $(el).attr('id');
});
没有直接的功能可以做到这一点。但是,使用 .map() 可以轻松完成。例如,
let ids = $('entry').map(function() {
return this.getAttribute('id');
}).get();
let ids = $('entry').map(function() {
return this.getAttribute('id');
}).get();
console.log(ids);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<entries>
<entry id="1" />
<entry id="2" />
<entry id="3" />
<entry id="4" />
</entries>
您可以使用 jQuery.fn.extend()
扩展 jQuery 并添加一个自定义方法来遍历所有匹配元素,从所有元素中提取一个或多个属性并将它们 pushes/merges分别返回array/object。
由于实现是 vanilla JS,这将比在所有中间步骤中使用 jQuery 对象和函数更快,但可能需要根据您需要支持的浏览器进行一些调整:
jQuery.fn.extend({
attrs: function(attributeName) {
if (attributeName) {
return this.toArray().map((el) => el.getAttribute(attributeName));
}
return this.toArray().reduce((merged, el) => {
const { attributes } = el;
const totalAttributes = attributes.length;
for (let i = 0; i < totalAttributes; ++i) {
const attribute = attributes[i].nodeName;
if (merged[attribute]) {
merged[attribute].push(el.getAttribute(attribute));
} else {
merged[attribute] = [el.getAttribute(attribute)];
}
}
return merged;
}, {});
},
});
console.log($('entry').attrs());
console.log($('entry').attrs('id'));
console.log($('entry').attrs('class'));
.as-console-wrapper {
max-height: 100% !important;
}
<entries>
<entry id="1" class="foo" />
<entry id="2" class="bar" />
<entry id="3" class="baz" />
<entry id="4" class="qux" />
</entries>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
这是使用 JavaScript Array .map() 函数的一种方法:
let ids = jQuery.makeArray($('entry')).map(entry => entry.id);
console.log('ids:', ids);
<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js"></script>
<entries>
<entry id="1" />
<entry id="2" />
<entry id="3" />
<entry id="4" />
</entries>