[英]jQuery - element is selected by attribute but the index returns value of the elements without attribute
[英]Detect elements with attribute without value
可以说我有
<div class="some-class" selected></div>
我想说是否已“选中” attr,但是
$(".some-class").attr("selected") //undefined
$(".some-class").is("*[selected]") // false
我能否说出即使没有价值也selected
attrbute吗?
试试看:编写一个函数hasAttr()
,检查提供的属性是否undefined
。 如果undefined
表示属性不存在。
$.fn.hasAttr = function(name) {
return this.attr(name) !== undefined;
};
if($('.some-class').hasAttr('selected'))
{
//do your stuff
}
你可以做
$(".some-class[selected]")
然后检查是否存在
let element = $(".some-class[selected]");
if (element.length)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.