gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 08:17:03 26 4
gpt4 key购买 nike

假设我有一个 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com