简体   繁体   English

如何从这行代码显示我的数组?

[英]How can I display my array from this line of code?

<!DOCTYPE html>
<html>

<body>

<script>

var a = 33;
var b = 6;

var famList = [a, b];

function famDisp (){
  for (var i=0, num=famList.length; i<num; i++){
    famList[i];
  }
}

famDisp(famList);

</script>

</body>
</html>

I would like to display the array famList but I am not getting any result.我想显示数组 famList 但我没有得到任何结果。 Using console.log to display I get this error "Uncaught TypeError: famList is undefined" What have I been doing wrong?使用 console.log 显示我收到此错误“Uncaught TypeError: famList is undefined”我做错了什么?

I think you missed parameter in famDisp () so replaced我想你错过了famDisp() 中的参数所以替换了

famDisp ()

TO

 famDisp (famList)

then it will be work perfectly然后它会完美地工作

 var a = 33; var b = 6; var famList = [a, b]; function famDisp (famList){ for (var i=0, num=famList.length; i<num; i++){ famList[i]; console.log(famList[i]) } } famDisp(famList);
 <!DOCTYPE html> <html> <body> Html code </body> </html>

If your question is where to write the console.log just write it wherever you wanted it to be in the script tag.如果您的问题是在哪里编写 console.log,只需将它写在您希望它在脚本标记中的任何位置。 but if you want to display the values in the browser window then the concept if "document.getlementby'id , class name or tagnmae '" should be applied.但是如果你想在浏览器窗口中显示值,那么应该应用“document.getlementby'id, class name or tagnmae '”的概念。

    <!DOCTYPE html>
<html>

<head>

</head>

<body>

    <h1 id="ht-tag">display the result here </h1>

    <script>

        var a = 33;
        var b = 6;

        var famList = [a, b];
        let num = famList.length
        function famDisp() {
            for (var i = 0; i < num; i++) {
                famList[i];
                console.log(famList[i])
                text = document.createTextNode(famList[i] + " ,")
                document.getElementById("ht-tag").appendChild(text)

            }
        }


        famDisp(famList);

    </script>
</body>

</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何修复代码以显示正确的数组索引 - How can I fix my code to display correct index of array 如何让我的代码等到我从服务器获得处理过的图像以在 Javascript 中显示 - How can I make my code wait until I got processed image from the server to display in Javascript 如何修改使用JavaScript代码显示的时间 - How can I modify the time I display with my JavaScript code 如何从命令行将参数传递给我的 Javascript 代码? - How can I pass an argument into my Javascript code from command line? 如何在博客博客上更好地显示代码? - How can I display code better on my blogger blog? 如何将我的数组数据显示到文本框中? - how can I display my array data into textboxes? 如何正确显示我的数组? 初学者内 - How can I correctly display my array? Beginner inside 在JSF 2.0应用程序中,如何从ManagedBean将大量数据输入到我的javascript代码中? - In a JSF 2.0 app, how can i get a huge array of data into my javascript code from the ManagedBean? 如何让 1 个数组包含许多 object 以从我的代码创建 JSON 文件? - How can I get 1 array contains many object to create a JSON file from my code? 如何从补间函数中调用数组以优化代码 - How can I call the array from the tween function to optimize my code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM