gpt4 book ai didi

javascript - 使用 Javascript if 语句显示或隐藏 div

转载 作者:行者123 更新时间:2023-11-28 16:10:11 25 4
gpt4 key购买 nike

对于 javascript,我是一个完全的初学者,我正在尝试创建一个简单的 if/else 语句来显示不同的 div。

<script>
if (window.FileReader && Modernizr.draganddrop){
alert("Your browser supports drag and drop!!");
document.getElementById(no).style.display = 'none';
}else{
alert("Sorry, browser does not support drag and drop!");
document.getElementById(yes).style.display = 'none';
}
</script>

然后,在体内

<div id="yes">Drag and drop yes</div>

<div id="no">Drag and drop no</div>

虽然 Modernizr 工作得很好,但脚本显示了两个 div

我们将不胜感激任何帮助

最佳答案

问题是 getElementById 需要一个字符串,据我所知您还没有声明是/否。

if (window.FileReader && Modernizr.draganddrop) {
alert("Your browser supports drag and drop!!");
document.getElementById('no').style.display = 'none';
} else {
alert("Sorry, browser does not support drag and drop!");
document.getElementById('yes').style.display = 'none';
}

关于javascript - 使用 Javascript if 语句显示或隐藏 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13224646/

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