js里怎么将 document.getElementById 改成调用 class 类的名称
这个里面直接将document.getElementById改成getElementsByClassName似乎就不能正常显示了。源码如下<scripttype="tex...
这个里面直接将document.getElementById 改成getElementsByClassName 似乎就不能正常显示了。
源码如下
<script type="text/javascript">
function test(obj) {
if (obj.id == "Radio1") {
document.getElementById("Div1").style.display = "block";
document.getElementById("Div2").style.display = "none";
document.getElementById("Div3").style.display = "none";
}
if (obj.id == "Radio2") {
document.getElementById("Div1").style.display = "none";
document.getElementById("Div2").style.display = "block";
document.getElementById("Div3").style.display = "none";
}
if (obj.id == "Radio3") {
document.getElementById("Div1").style.display = "none";
document.getElementById("Div2").style.display = "none";
document.getElementById("Div3").style.display = "block";
}
}
</script>
<input name="" type="button" id="Radio1" onclick="test(this)" value="我" />
<input id="Radio2" name="" type="button" onclick="test(this)" value="你" />
<div id="Div1" class="" style="display:none;">aaaa</div>
<div id="Div2" class="" style="display:none;">bbbb</div> 展开
源码如下
<script type="text/javascript">
function test(obj) {
if (obj.id == "Radio1") {
document.getElementById("Div1").style.display = "block";
document.getElementById("Div2").style.display = "none";
document.getElementById("Div3").style.display = "none";
}
if (obj.id == "Radio2") {
document.getElementById("Div1").style.display = "none";
document.getElementById("Div2").style.display = "block";
document.getElementById("Div3").style.display = "none";
}
if (obj.id == "Radio3") {
document.getElementById("Div1").style.display = "none";
document.getElementById("Div2").style.display = "none";
document.getElementById("Div3").style.display = "block";
}
}
</script>
<input name="" type="button" id="Radio1" onclick="test(this)" value="我" />
<input id="Radio2" name="" type="button" onclick="test(this)" value="你" />
<div id="Div1" class="" style="display:none;">aaaa</div>
<div id="Div2" class="" style="display:none;">bbbb</div> 展开
1个回答
展开全部
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>
RunJS 演示代码
</title>
<script type="text/javascript">
function test(obj) {
if (obj.id == "Radio1") {
document.querySelector(".Div1").style.display = "block";
document.querySelector(".Div2").style.display = "none";
}
if (obj.id == "Radio2") {
document.querySelector(".Div1").style.display = "none";
document.querySelector(".Div2").style.display = "block";
}
}
</script>
</head>
<body>
<input type="button" id="Radio1" onclick="test(this)" value="我" />
<input id="Radio2" type="button" onclick="test(this)" value="你" />
<div class="Div1" style="display:none;">
aaaa
</div>
<div class="Div2" style="display:none;">
bbbb
</div>
</body>
</html>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询