我有一些简单的 HTML:
<div>Below is the div id containing the attorney value. </div>
<div id="attorney"> 0 </div>
<div id="profiletab1">THIS IS THE DIV I WANT TO HIDE</div>
在页面加载时,当 div.attorney 的值 = '0' 时,我想更改 div.profiletab1
css display style="none"
。
我不确定我错过了什么。
<script type="text/javascript">
function myfunc(){
var atty= document.getElementById("attorney").value;
if (atty=== '0'){
document.getElementById("profiletab1").style.display = "none";
}
}
window.onload = myfunc;
</script>
最佳答案
您需要获取 textContent,而不是值并 trim 它,因为那里有空格:
function myfunc() {
var atty = document.getElementById("attorney").textContent.trim();
if (atty === '0') {
document.getElementById("profiletab1").style.display = "none";
}
}
关于javascript - 将文本存储为变量然后根据变量值更改 css 样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50260227/