请帮助我完成这个简单的脚本
我是 JavaScript 新手,想就我的简单脚本寻求一些帮助。
我想做的是借助 (for) 循环检索并显示无序列表中所有列表项元素的值。我能够让脚本一一显示警报窗口中的所有列表项。但问题是我需要以表行方式显示所有列表元素的值。像这样:
星期一
星期二
星期三
.......
这是我的脚本中的内容:
<script language="JavaScript">
<!--
function process() {
a = document.getElementsByTagName('li')
for (i = 0; i < a.length; i++) {
alert(a[i].childNodes[0].nodeValue);
}
}
//-->
</script>
这是 HTML 代码:
<body>
<ul>
<li>Monday</li>
<li>Tuesday</li>
<li>Wednesday</li>
</ul>
<input type="button" value="Submit" onclick="process()" />
</body>
如果可能的话,是否有人也请解释一下我的脚本中哪里出了问题?为什么所有 3 个列表项值无法同时显示在警报窗口中?
多谢!
I am new to JavaScript and would like to ask for some help with my simple script.
What I am trying to do is to retrieve and display the values of all list item elements in the unordered list with the help of the (for) loop. I was able to get the script display all list items in the alert window one by one. But the problem is that I need values of all list elements displayed in a table row way. Like this:
Monday
Tuesday
Wednesday
.......
Here is what I have in my script:
<script language="JavaScript">
<!--
function process() {
a = document.getElementsByTagName('li')
for (i = 0; i < a.length; i++) {
alert(a[i].childNodes[0].nodeValue);
}
}
//-->
</script>
And here is HTML code:
<body>
<ul>
<li>Monday</li>
<li>Tuesday</li>
<li>Wednesday</li>
</ul>
<input type="button" value="Submit" onclick="process()" />
</body>
If that's possible at all would anyone please also explain where I am wrong in my script? Why all 3 list item values can't be shown in the alert window at once?
Thanks a lot!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
首先,创建一个字符串变量:
var all_at_once = ""
。然后,添加nodeValue
的内容。最后,警告这个变量:First, create a string variable:
var all_at_once = ""
. Then, add the contents of thenodeValue
. Finally, alert this variable:alert
会重复显示,因为这就是for
循环的作用...它会循环!该循环将迭代 getElementsByTagName 返回的元素数组,为该数组中的每个元素执行一次循环体。如果您想显示一个警报,一个选项是构建一个包含适当文本的字符串,然后警报它:
关于代码的一些其他注释...您几乎应该始终使用以下方式声明变量:
var
关键字以防止它们泄漏到全局范围内。您还应该始终以分号结束行:The
alert
shows repeatedly because that is what afor
loop does... it loops! The loop will iterate over the array of elements returned bygetElementsByTagName
, executing the loop body once for each element in that array.If you wanted to display one alert, an option would be to build up a string containing the appropriate text, and
alert
it afterwards:Some other notes on your code... you should almost always declare variables with the
var
keyword to prevent them leaking into the global scope. You should also always end lines with semi-colons:如果您需要 1 个包含所有文本的弹出窗口,则只需调用一次警报。
You need to call alert only once if you need 1 popup with all the text.
您可以以任何您喜欢的方式处理日期,首先将它们存储在数组中,然后迭代:
请参阅:http: //jsfiddle.net/jkeyes/Cfg4k/ 作为一个工作示例。
You can process the days in whatever manner you like by storing them in an array first, and then iterating:
See: http://jsfiddle.net/jkeyes/Cfg4k/ for a working example.
对您的函数进行这些少量调整应该会产生您想要的结果。祝你好运!
更改内容: 1) 设置一个空字符串 var 2) 无需提醒每个值,只需将它们附加到您之前创建的字符串 var 3) 最后,提醒新创建的(连接的)字符串!
现在我看到自从打开这个线程以来已经有大量的答案......但也许所有不同的解决方案都会以不同的方式帮助您。
These few adjustments to your function should produce the result you want. Good luck!
What changed: 1) Set up an empty string var 2) Instead of alerting each value, just append them to the string var you created earlier 3) Finally, alert the newly created (concatenated) string!
Now I see there have been tons of answers since opening this thread... but maybe all the different solutions will help you in different ways.