我正在重新配置一个包含 100 多个输入元素的复杂 HTML 文档。我想快速找到任何具有空白 id ( <input type="text" name="something" id="" ...
)、仅包含空格的 id ( <button name="unimportant" id=" "...
) 或根本不知道 ( <select name="whatever">
) 的元素,以便我可以进行必要的更正来源。
我知道空白 ID 的选择器是 $('[id=""]')
,但是如何使用一个选择器(或串联选择器)获取所有目标元素?
示例 HTML:
<form id="provision">
<input type="text" name="something" id="" value="blank id" /><br />
<input type="text" name="something2" value="no id" /><br />
<select name="something3"><option>No id</option></select><br />
<textarea name="something4" id="">Blank id</textarea><br />
<button name="something5" id=" ">whitespace id</button><br />
<button name="something6" id=" ">extra whitespace id</button><br />
<label><input type="radio" name="something7" id="
">Carriage return id</label>
</form>
请参阅http://jsfiddle.net/jhfrench/VNdL6/例如添加红色边框 ( .css('border-color', 'red')
)。
最佳答案
用逗号分隔选择器:
$("[id=''], [id=' '], :not([id])").css("border-color", "red");
另一种选择是使用过滤:
$("[id]").filter(function() {
return $.trim(this.id) === "";
}).add(":not([id])").css("border-color", "red");
关于javascript - 如何返回没有 id、空 id 或空白 id 的元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14363365/