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

编程入门 行业动态 更新时间:2024-10-13 04:21:33
本文介绍了jQuery属性选择器:[x = y]或[x = z]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有以下jQuery选择器:

I have the following jQuery selector:

$("a[href^=''],a[href^='']");

是否可以更改此设置,这样我就不需要重复指定a[href^=了?

Is it possible to change this so that I don't need to specify a[href^= twice?

例如,类似:

$("a[href^=''||'']");

编辑:我的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^=""],[href^=""]');

或带有令牌:

var startsWith = ['', '']; $('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(,)')

更多推荐

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

本文发布于:2023-11-22 01:48:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1615501.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:属性   选择器   jQuery

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!