jQuery属性选择器:[x = y]或[x = z] [英] jQuery attribute selector: [x=y] or [x=z]
本文介绍了jQuery属性选择器:[x = y]或[x = z]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下jQuery选择器:
I have the following jQuery selector:
$("a[href^='http://'],a[href^='https://']");
是否可以更改此设置,这样我就不需要重复指定a[href^=
了?
Is it possible to change this so that I don't need to specify a[href^=
twice?
例如,类似:
$("a[href^='http://'||'https://']");
编辑:我的http
和https
示例不应从字面上理解.我可能正在寻找以y
和z
开头的值.
My example of http
and https
should not be taken literally. I could be looking for values starting with y
and z
instead.
推荐答案
如果您愿意使用第二个函数调用,则非常简单:
Quite simple if you're willing to use a second function call:
$('a').filter('[href^="http://"],[href^="https://"]');
或带有令牌:
var startsWith = ['http://', 'https://'];
$('a').filter(function () {
var i;
for (i = 0; i < startsWith.length; i++) {
if ($(this).is('[href^="' + startsWith[i] + '"]')) {
return true;
}
}
return false;
});
或使用自定义表达式:
$.expr[':'].hrefStartsWith = function (ele, i, info) {
var tokens;
tokens = info[3].split(',');
for (i = 0; i < tokens.length; i++) {
if ($(ele).is('[href^="' + tokens[i] + '"]')) {
return true;
}
}
return false;
};
将用作:
$('a:hrefStartsWith(http://,https://)')
这篇关于jQuery属性选择器:[x = y]或[x = z]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文