为什么第一段工作,第二段没有,两者都有来自脚本的相同调用 [英] Why first paragraph work and second no and both with the same call from script
本文介绍了为什么第一段工作,第二段没有,两者都有来自脚本的相同调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
<!DOCTYPE html>
< html>
< head>
< meta http-equiv =Content-Typecontent =text / html; charset = utf-8>
< title> Javascript Learning< / title>
< script type =text / javascript>
函数my(){
var userName =Lister;
switch(userName){
caseLister:
document.getElementById(bbref)。innerHTML =Lister Is the Man!;
休息;
caseRimmer:
document.getElementById(bbref)。innerHTML =Rimmer是一个Smeg头......;
休息;
默认值:
doacument.getElementById(bbref)。innerHTML =它全都是关于\Cat \
}
var x = document.cal。元素[0]。名称;
var y =jan;
document.write(x);
switch(y){
casejan:
document.getElementById(1)。innerHTML =1;
休息;
默认值:
document.write(sdffds);
}
}
window.onload = my;
< / script>
< / head>
< body>
< h1 id =bbref>测试标题< / h1>
< h2 id =bbref> AAAAAA< / H2>
< / body>
< / html>
我尝试过:
两个段落与脚本相同的调用
解决方案
嗯,目前还不是很清楚你在说什么,但是。 ..doacument
绝对错误:
doacument.getElementById( bbref)。innerHTML = 这一切都是关于\The Cat \
试试
function my(){
var userName =Lister;
var value ='';
switch(userName){
caseLister:
value =Lister Is the Man!;
休息;
caseRimmer:
value =Rimmer是一个Smeg头......;
休息;
默认值:
value =这都是关于\Cat \;
}
var elems = document.querySelectorAll('[id =bbref]');
for(var i = 0; i< elems.length; i ++){
elems [i] .innerHTML = value;
}
}
或
<!DOCTYPE html>
< html>
< head>
< meta http-equiv =Content-Typecontent =text / html; charset = utf-8>
< title> Javascript Learning< / title>
< script type =text / javascript>
函数my(){
var userName =Lister;
var value ='';
switch(userName){
caseLister:
value =Lister Is the Man!;
休息;
caseRimmer:
value =Rimmer是一个Smeg头......;
休息;
默认值:
value =这都是关于\Cat \;
}
var elems = document.getElementsByClassName('bbref');
for(var i = 0; i< elems.length; i ++){
elems [i] .innerHTML = value;
}
}
window.onload = my;
< / script>
< / head>
< body>
< h1 id =bbrefclass =bbref>测试标题< / h1>
< h2 id =bbrefclass =bbref> AAAAAA< / H2>
< / body>
< / html>
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Javascript Learning</title> <script type="text/javascript"> function my(){ var userName = "Lister"; switch (userName) { case "Lister" : document.getElementById("bbref").innerHTML = "Lister Is the Man!"; break; case "Rimmer": document.getElementById("bbref").innerHTML = "Rimmer is a Smeg head..."; break; default : doacument.getElementById("bbref").innerHTML = "It's all about \"The Cat\"" } var x = document.cal.elements[0].name; var y = "jan"; document.write(x); switch(y){ case "jan" : document.getElementById("1").innerHTML="1"; break; default : document.write("sdffds"); } } window.onload = my; </script> </head> <body> <h1 id="bbref">Test Title</h1> <h2 id="bbref"> aaaaaa</h2> </body> </html>
What I have tried:
the two paragraph with the same call from script
解决方案
Well, it's not very clear what you are talking about, but ...doacument
is definitely wrong:
doacument.getElementById("bbref").innerHTML = "It's all about \"The Cat\""
try
function my(){ var userName = "Lister"; var value = ''; switch (userName) { case "Lister" : value = "Lister Is the Man!"; break; case "Rimmer": value= "Rimmer is a Smeg head..."; break; default : value = "It's all about \"The Cat\""; } var elems = document.querySelectorAll('[id="bbref"]'); for (var i = 0; i < elems.length; i++) { elems[i].innerHTML = value; } }
or
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Javascript Learning</title> <script type="text/javascript"> function my() { var userName = "Lister"; var value = ''; switch (userName) { case "Lister": value = "Lister Is the Man!"; break; case "Rimmer": value = "Rimmer is a Smeg head..."; break; default: value = "It's all about \"The Cat\""; } var elems = document.getElementsByClassName('bbref'); for (var i = 0; i < elems.length; i++) { elems[i].innerHTML = value; } } window.onload = my; </script> </head> <body> <h1 id="bbref" class="bbref">Test Title</h1> <h2 id="bbref" class="bbref"> aaaaaa</h2> </body> </html>
这篇关于为什么第一段工作,第二段没有,两者都有来自脚本的相同调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文