![](/img/trans.png)
[英]How to hide the DOM element with ngShow/ngHide when condition is not met?
[英]Javascript: Hide element if condition is met
我试图根据其ID隐藏元素,但是,我无法让它工作。 我用Google搜索了答案,但无论我做什么,我似乎无法让它为我工作。
我的代码附在下面。
<!DOCTYPE html>
<html>
<!-- HTML -->
<body>
<div id="role" value="Edit">
<button> Edit </button>
</div>
</body>
<!-- Javascript -->
<script>
if(document.getElementById("role").value == 'Edit'){
document.getElementById("role").style.display = 'none';
}
</script>
</html>
您可以使用下面的代码,您想获得属性而不是值。
如果需要,可以使用querySelector而不是getElementById。
var role = document.querySelector('#role'); if(role.getAttribute('value') == 'Edit'){ role.style.display = 'none'; }
<!DOCTYPE html> <html> <!-- HTML --> <body> <div id="role" value="Edit"> <button> Edit </button> </div> </body> </html>
你试过这个吗?
<!DOCTYPE html>
<html>
<!-- HTML -->
<body>
<div id="role" value="Edit">
<button> Edit </button>
</div>
</body>
<!-- Javascript -->
<script>
if(document.getElementById("role").getAttribute('value') == 'Edit'){
document.getElementById("role").style.display = 'none';
}
</script>
在您的示例document.getElementById("role").value
中.value是未定义的。 要检索属性值,您应该使用element.getAttribute('attr')
方法或element.attributes[attr].value
。
在你的情况下,它将是document.getElementById("role").attributes['value'].value
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.