简体   繁体   English

jQuery属性选择器:[x = y]或[x = z]

[英]jQuery attribute selector: [x=y] or [x=z]

I have the following jQuery selector: 我有以下jQuery选择器:

$("a[href^='http://'],a[href^='https://']");

Is it possible to change this so that I don't need to specify a[href^= twice? 是否可以更改此设置,以便我不需要指定a[href^=两次?

For example, something like: 例如,类似于:

$("a[href^='http://'||'https://']");

EDIT: My example of http and https should not be taken literally. 编辑:我的httphttps例子不应该按字面意思。 I could be looking for values starting with y and z instead. 我可能正在寻找以yz开头的值。

Quite simple if you're willing to use a second function call: 如果你愿意使用第二个函数调用,那很简单:

$('a').filter('[href^="http://"],[href^="https://"]');

Or with tokens: 或者使用令牌:

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;
});

Or with a custom expression: 或者使用自定义表达式:

$.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;
};

Which would be used as: 哪个用作:

$('a:hrefStartsWith(http://,https://)')

Fiddle : http://jsfiddle.net/X8CYQ/ 小提琴http//jsfiddle.net/X8CYQ/


You should be able to use something like this: 你应该可以使用这样的东西:

$.expr[':'].exturl = function(obj) {
   return (/^https?:\/\//i).test($(obj).attr("href"));
}

Then use the selector like this: 然后像这样使用选择器:

$("a:exturl")

HTML : HTML

<a href="http://www.google.com">External link #1</a><br />
<a href="ftp://www.donotinclude.dk">No match link #1</a><br />
<a href="?nope">No match link #2</a><br />
<a href="https://www.google.dk">External link #2</a><br />
<a href="http://www.google.se">External link #3</a><br />
<a href="ssh://whywouldyouevendothis">No match link #3</a><br />
<a href="https://www.google.co.uk">External link #4</a><br />
<a href="http://www.facebook.com">External link #5</a><br />
<a href="/index.html">No match link #4</a><br />
<a href="https://www.facebook.com">External link #6</a><br />​

JS : JS

$.expr[':'].exturl = function(obj) {
    return (/^https?:\/\//i).test($(obj).attr("href"));
}
$(document).ready(function() {
    $("a:exturl").css("color", "red");
});​

You can simply do 你可以干脆做

$("a[href^='http']")

As the href^= just means "starts with" unless you have a need to be sure :// is included as well. 由于href^=仅表示“以...开头”,除非您需要确定://也包括在内。

Here is an example with other attributes and values: 以下是其他属性和值的示例:

http://jsbin.com/owehow/2/edit http://jsbin.com/owehow/2/edit

You can't do this in jQuery by default .. it doesn't have any sort of "or" condition for selectors (except for the comma, sort of, which is what you want to avoid). 默认情况下,您无法在jQuery中执行此操作。它对选择器没有任何“或”条件(逗号除外,排序,这是您要避免的)。

You could create a custom filter if you wanted to: 如果您想要创建自定义过滤器,可以:

http://jsfiddle.net/yJZDg/ http://jsfiddle.net/yJZDg/

This is very overkill unless you need to do this kind of thing a lot . 除非你需要做很多这样的事情,否则这是非常矫枉过正的。 The example will also only work for an exact match. 该示例也仅适用于完全匹配。 You would have to tweak for a "starts with" or some other kind of match, but the idea is there. 你必须调整“开始时”或其他类型的匹配,但这个想法就在那里。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM