javascript - 将文本存储为变量然后根据变量值更改 css 样式

标签 javascript html css function

我有一些简单的 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/

相关文章:

javascript - 在变量上保存异步/等待响应

javascript - 导入的组件文件在使用 es6 的 AngularJS 应用程序中不起作用

html - 如何以相同的方式垂直对齐标签和按钮内的图标

javascript - 第三方 packageas 中 "require(./....."的 Webpack 捆绑问题

HTML/CSS 标签左 : 10px not working

html - XPath选择不只有一个特定子节点的节点?

javascript - 激活时如何删除文本装饰。我有以下代码,但链接带有下划线

javascript - Chrome 中的 Superslides CSS 问题 - 需要 CSS 专家

html - 无论是否溢出,都将子 div 固定在其父级中

javascript - 正则表达式 - 匹配除单个数字之外的所有字母数字字符