Cur*_*urt 9 jquery jquery-selectors
我有以下jQuery选择器:
$("a[href^='http://'],a[href^='https://']");
Run Code Online (Sandbox Code Playgroud)
是否可以更改此设置,以便我不需要指定a[href^=两次?
例如,类似于:
$("a[href^='http://'||'https://']");
Run Code Online (Sandbox Code Playgroud)
编辑:我的例子http,并https不能按字面意思理解.我可以找值开始y和z代替.
如果您愿意使用第二个函数调用,则非常简单:
\n\n$(\'a\').filter(\'[href^="http://"],[href^="https://"]\');\nRun Code Online (Sandbox Code Playgroud)\n\n或者使用令牌:
\n\nvar startsWith = [\'http://\', \'https://\'];\n$(\'a\').filter(function () {\n var i;\n for (i = 0; i < startsWith.length; i++) {\n if ($(this).is(\'[href^="\' + startsWith[i] + \'"]\')) {\n return true;\n }\n }\n return false;\n});\nRun Code Online (Sandbox Code Playgroud)\n\n或者使用自定义表达式:
\n\n$.expr[\':\']\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b.hrefStartsWith = function (ele, i, info) {\n var tokens;\n tokens = info[3].split(\',\');\n for (i = 0; i < tokens.length; i++) {\n if ($(ele).is(\'[href^="\' + tokens[i] + \'"]\')) {\n return true;\n }\n }\n return false;\n};\nRun Code Online (Sandbox Code Playgroud)\n\n它将用作:
\n\n$(\'a:hrefStartsWith(http://,https://)\')\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
838 次 |
| 最近记录: |