我可以在 jQuery 中查询属性键通配符吗?
我想选择具有以设置字符串开头的属性名称(键)的元素。用例:jQuery UI 对话框 创建按钮,其唯一唯一标识符是带有顺序值,例如 jQuery1288273859637="40"
我想根据属性的 name 是 jQuery*(以 jQuery 开头)这一事实进行选择
I'd like to select elements with attribute names (keys) that begin with a set string. Use case: the jQuery UI dialog creates buttons whose only unique identifers are a pseudo-random custom attribute with a sequential value such as jQuery1288273859637="40"
I'd like to select based on the fact that the attribute's name is jQuery* (begins with jQuery)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我唯一能想到的就是测试
.filter() 中每个 DOM 元素的各个键
。这将是非常低效的,但如果你真的想要它,它可能看起来像这样:
这将循环遍历
中的所有元素,然后循环在每个键/值属性上,测试每个键以查看它是否以
"jQuery"
开头。如果是,则返回true
,并且循环被破坏。如果不是,则在测试所有属性后返回false
。我会找到另一种方式。说真的,不要这样做。
Only think I could think of would be to test the individual keys of every DOM element in a
.filter()
.This would be horribly inefficient, but if you really wanted it, it could look something like this:
This will loop over all elements in the
<body>
, and then loop over the key/value properties of each, testing each key to see if it starts with"jQuery"
. If so, it returnstrue
, and the loop is broken. If not, it returnsfalse
after all properties have been tested.I'd find another way. Seriously, don't do this.
您正在查看的属性是 jQuery Expando ...将这些属性作为目标是完全没有用的,因为“jQuery”后面的数字基于当前时间并随着每个页面重新加载而变化(ref1 & ref2)。
最好在基本 HTML 中定位一个类。例如,jQuery UI 对话框应具有以下基本 HTML (ref):
查找 id 或您想要定位的类并使用它!
如果您需要定位对话框中的按钮,请使用如下内容:
The attribute you are looking at is the jQuery expando ... it would be completely useless to target these attributes as the number after the "jQuery" is based on the current time and changes with each page reload (ref1 & ref2).
It would be best to target a class in your base HTML. For example, the jQuery UI Dialog should have this base HTML (ref):
Find the id or class you want to target and use it instead!
If you need to target a button in the dialog, then use something like this:
获取对对话框的引用,然后使用 css 选择器查找按钮。 firebug(或谷歌浏览器开发者工具)对于确定要查找的内容非常有用。
Get an reference to the dialog, and then use a css selector to find the button. firebug (or google chrome developer tools) are useful for determining what to look for.