I have this element:
<div class="column" style="background: green;" used="used"></div>
How can I check if the div has the 'used' property = 'used'?
I am asking how to do this in javascript not in jQuery.
使用hasAttribute
document.getElementById("divid").hasAttribute("used");
You can check it by using getAttribute(attributeName)
function,
var elem = document.querySelector(".colum[user]");
var hasUsed = elem.getAttribute("used") == "used";
console.log(hasUsed); // true/false
You can do checking like this:
if (document.getElementsByClassName("column")[0].getAttribute("used")=="used"){ alert('it has the "used" property.') }
<div class="column" style="background: green;" used="used"></div>
HTML DOM hasAttribute() Method
var elemArr = document.getElementsByClassName("column");
for(i = 0; i < elemArr.length; i++){
if (elem[i].hasAttribute("used")){
alert('it has the "used" property.');
}
}
remember you are dealing with a class and could be present in more than one element...
You can iterate only div elements with getElementByTagName
var divs = document.getElementsByTagName("div");
for(var i = 0; i < divs.length; i++){
if(divs[i].getAttribute("used") == "used"){
alert(divs[i].id);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.