请帮助我完成这个简单的脚本

发布于 2024-12-08 11:04:36 字数 834 浏览 0 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

悲喜皆因你 2024-12-15 11:04:36

首先,创建一个字符串变量:var all_at_once = ""。然后,添加 nodeValue 的内容。最后,警告这个变量:

function process(){
    var a = document.getElementsByTagName('li')
    var all_at_once = "";
    for(i=0;i<a.length;i++){
        all_at_once += a[i].childNodes[0].nodeValue + " ";
    }
    alert(all_at_once);
}

First, create a string variable: var all_at_once = "". Then, add the contents of the nodeValue. Finally, alert this variable:

function process(){
    var a = document.getElementsByTagName('li')
    var all_at_once = "";
    for(i=0;i<a.length;i++){
        all_at_once += a[i].childNodes[0].nodeValue + " ";
    }
    alert(all_at_once);
}
半枫 2024-12-15 11:04:36

alert 会重复显示,因为这就是 for 循环的作用...它会循环!该循环将迭代 getElementsByTagName 返回的元素数组,为该数组中的每个元素执行一次循环体。

如果您想显示一个警报,一个选项是构建一个包含适当文本的字符串,然后警报它:

var yourString = "";
for(i=0;i<a.length;i++){
    yourString += a[i].childNodes[0].nodeValue;
}
alert(yourString);

关于代码的一些其他注释...您几乎应该始终使用以下方式声明变量: var 关键字以防止它们泄漏到全局范围内。您还应该始终以分号结束行:

function process(){
    var a = document.getElementsByTagName('li'),
        yourString = "";
    for(i=0;i<a.length;i++){
        yourString += a[i].childNodes[0].nodeValue;
    }
    alert(yourString);
}

The alert shows repeatedly because that is what a for loop does... it loops! The loop will iterate over the array of elements returned by getElementsByTagName, 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:

var yourString = "";
for(i=0;i<a.length;i++){
    yourString += a[i].childNodes[0].nodeValue;
}
alert(yourString);

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:

function process(){
    var a = document.getElementsByTagName('li'),
        yourString = "";
    for(i=0;i<a.length;i++){
        yourString += a[i].childNodes[0].nodeValue;
    }
    alert(yourString);
}
对你而言 2024-12-15 11:04:36
<script language="JavaScript">
<!--

    function process(){
        var data = '';
        a=document.getElementsByTagName('li')

            for(i=0;i<a.length;i++){

                    data = data  + '\n' +(a[i].childNodes[0].nodeValue);
            }
         alert(data);
    }


//-->
</script>

如果您需要 1 个包含所有文本的弹出窗口,则只需调用一次警报。

<script language="JavaScript">
<!--

    function process(){
        var data = '';
        a=document.getElementsByTagName('li')

            for(i=0;i<a.length;i++){

                    data = data  + '\n' +(a[i].childNodes[0].nodeValue);
            }
         alert(data);
    }


//-->
</script>

You need to call alert only once if you need 1 popup with all the text.

人疚 2024-12-15 11:04:36
function process()
{
  var a = getElementsByTagName('li'),
      text = '';

  for( i = 0; i < a.length; i++ )
  {
    text += a[i].childNodes[0].nodeValue + '\n';
  }

  alert( text );
}
function process()
{
  var a = getElementsByTagName('li'),
      text = '';

  for( i = 0; i < a.length; i++ )
  {
    text += a[i].childNodes[0].nodeValue + '\n';
  }

  alert( text );
}
久夏青 2024-12-15 11:04:36

您可以以任何您喜欢的方式处理日期,首先将它们存储在数组中,然后迭代:

var days = new Array();
var a = document.getElementsByTagName('li')
for(var i = 0; i < a.length; i++) {
    days.push(a[i].childNodes[0].nodeValue);
}

for (i=0; i < days.length; i++) {
    // process the day
}

请参阅: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:

var days = new Array();
var a = document.getElementsByTagName('li')
for(var i = 0; i < a.length; i++) {
    days.push(a[i].childNodes[0].nodeValue);
}

for (i=0; i < days.length; i++) {
    // process the day
}

See: http://jsfiddle.net/jkeyes/Cfg4k/ for a working example.

冰雪梦之恋 2024-12-15 11:04:36

对您的函数进行这些少量调整应该会产生您想要的结果。祝你好运!
更改内容: 1) 设置一个空字符串 var 2) 无需提醒每个值,只需将它们附加到您之前创建的字符串 var 3) 最后,提醒新创建的(连接的)字符串!

function process() {
    a = document.getElementsByTagName('li');
    var days = new String("");

    for (i = 0; i < a.length; i++) {
        days = days+(a[i].childNodes[0].nodeValue)+"\n";
    }
    alert(days);
}

现在我看到自从打开这个线程以来已经有大量的答案......但也许所有不同的解决方案都会以不同的方式帮助您。

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!

function process() {
    a = document.getElementsByTagName('li');
    var days = new String("");

    for (i = 0; i < a.length; i++) {
        days = days+(a[i].childNodes[0].nodeValue)+"\n";
    }
    alert(days);
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文