js问题,代码如下,为什么运行后的时候,for只循环一次啊……大侠
<htmllang="zh"><head><scripttype="text/javascript">functionbb(){varx=document.getElem...
<html lang="zh"><head>
<script type="text/javascript">
function bb(){
var x=document.getElementsByTagName('p');
for (var i = 0; i < x.length; i++) {
document.write(x[i].firstChild.nodeValue);
};
}
</script>
<style type="text/css">
p{
font-size: 30px;
background-color: #DDD;
width: 1000px;
}
</style>
</head>
<body>
<button onclick="bb()">click</button>
<p id="txt1">Hello world....This is a text area1>></p>
<p id="txt1">Hello world....This is a text area2>></p>
<p id="txt1">Hello world....This is a text area3>></p>
</body>
</html> 展开
<script type="text/javascript">
function bb(){
var x=document.getElementsByTagName('p');
for (var i = 0; i < x.length; i++) {
document.write(x[i].firstChild.nodeValue);
};
}
</script>
<style type="text/css">
p{
font-size: 30px;
background-color: #DDD;
width: 1000px;
}
</style>
</head>
<body>
<button onclick="bb()">click</button>
<p id="txt1">Hello world....This is a text area1>></p>
<p id="txt1">Hello world....This is a text area2>></p>
<p id="txt1">Hello world....This is a text area3>></p>
</body>
</html> 展开
2个回答
展开全部
如yanzilisan183所说,第一次输出后页面中的p标签就被文本覆盖了,稍作修改即可:
<html lang="zh">
<head>
<script type="text/javascript">
function bb() {
var x = document.getElementsByTagName('p');
// 把内容缓存起来 --------------------
var length = x.length, content = '';
for(var i = 0; i < length; i++) {
content += x[i].firstChild.nodeValue;
};
document.write(content);
// --------------------
}
</script>
<style type="text/css">
p {
font-size: 30px;
background-color: #DDD;
width: 1000px;
}
</style>
</head>
<body>
<button onclick="bb()">
click
</button>
<p id="txt1">
Hello world....This is a text area1>>
</p>
<p id="txt1">
Hello world....This is a text area2>>
</p>
<p id="txt1">
Hello world....This is a text area3>>
</p>
</body>
</html>
<html lang="zh">
<head>
<script type="text/javascript">
function bb() {
var x = document.getElementsByTagName('p');
// 把内容缓存起来 --------------------
var length = x.length, content = '';
for(var i = 0; i < length; i++) {
content += x[i].firstChild.nodeValue;
};
document.write(content);
// --------------------
}
</script>
<style type="text/css">
p {
font-size: 30px;
background-color: #DDD;
width: 1000px;
}
</style>
</head>
<body>
<button onclick="bb()">
click
</button>
<p id="txt1">
Hello world....This is a text area1>>
</p>
<p id="txt1">
Hello world....This is a text area2>>
</p>
<p id="txt1">
Hello world....This is a text area3>>
</p>
</body>
</html>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询