javascript - 如何根据 javascript 变量显示网站的特定部分?

标签 javascript

假设我有一个 javascript 变量“is_valid”

如果变量为 1,我想显示:

<div>It's valid!</div> ...and a big chunk of other code

否则...

<div>NOT valid</div>...and a big chunk of other code

我不想使用 INNERHTML。我想这样做:

<script type="text/javascript">
    if(is_valid == 1){
</script>
    It's valid!
<script type="text/javascript">
    }else{
</script>
    It's not valid
<script type="text/javascript">
    }
</script>

最佳答案

将代码放在可以显示或隐藏的元素中。示例:

<div id="Valid">
  <div>It's valid!</div> ...and a big chunk of other code
</div>
<div id="Invalid">
   <div>NOT valid</div>...and a big chunk of other code
</div>

<script type="text/javascript">
document.getElementById('Valid').style.display = (is_valid == 1 ? 'block' : 'none');
document.getElementById('Invalid').style.display = (is_valid == 1 ? 'none' : 'block');
</script>

关于javascript - 如何根据 javascript 变量显示网站的特定部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1616049/

相关文章:

Javascript,数组

javascript - 整理Js项目文件

javascript - 在 React 和 jQuery 中获取鼠标坐标

javascript - setInterval 中引用成员函数

javascript - 如何在 bootstrap 下拉菜单中添加彩色边框

javascript - Zclip 在 jsfiddle 上工作但不能在本地工作?

javascript - 为右侧菜单添加 javascript 时,左侧的子菜单不会出现。但为什么?

javascript - 关联绘图图和 Angular JS 数据

javascript - JS : EcmaScript6 how to pass different number of parameters to extended class

javascript - 如何在 IE 上播放 .wav 文件?