我不知道是否可行,但我想根据元素的属性名称前缀查找元素。该属性如下所示:
<div attName:someOtherValue="12"> </div>
它不一定非要在 div 上,但它确实遵循上述模式。
更新
好的,我忘记了关于我要完成的事情的 2 个要点: 1. 我不会使用 jQuery,所以我将只使用原生 JS。 2. 2. 冒号后的 par 发生变化,所以当我说我需要根据属性前缀查找时,我不能使用冒号之后的部分,只能使用冒号之前的部分(或包括冒号)
最佳答案
你可以使用html5的数据属性
<div data-theNameYouWant="someValue">xpto</div>
和 jQuery
$("*").data("theNameYouWant", function(){
//do your stuff
});
//in case you want a log of that value
console.log($("*").data("theNameYouWant"));
//this will return a log (in browser console), with all the elements values that contains the attribute "data-theNameYouWant"
关于javascript - 查找包含特定属性前缀的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29367606/