在html中调用外部.js文件 [英] call external .js file in html

查看:142
本文介绍了在html中调用外部.js文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 array.js 文件和一个 index.html



我的 array.js 文件如下所示:

  function go(){
var array = new Array();
array [0] =红色;
array [1] =蓝色;
array [3] =绿色;
for(var i = 0; i< array.length; i ++){
document.write(< li> + array [i] +< br />) ;


code
$ b我的 index.html
> / code>文件看起来像这样:

 < html> 
< head>
< script type =text / javascriptsrc =array.js>< / script>
< / head>
< body>
< input type =buttononclick =go()value =Display JS Array/>
< script>
go();
< / script>
< / body>
< / html>

当我点击显示JS Array 按钮在HTML页面上,没有任何反应。
我想单击按钮后显示数组元素。
应该如下所示:




  • 红色

  • 蓝色

  • Green


解决方案

替换 document.write (< li> + array [i] +< br />); with document.write(< li> i] +< / li>);



在您的HTML文件中,移除

 < script> 
go();
< / script>

因为已经调用了go();功能onclick。在你的数组中,数组[2]会给出未定义的。



试试这个对我有用:

 < html> 
< head>
< script type =text / javascript>
函数go(){
var array = new Array();
array [0] =红色;
array [1] =蓝色;
array [2] =绿色;
li = document.getElementById(list);
li_arr =;
for(var i = 0; i< array.length; i ++){
li_arr + =< li> + array [i] +< / li>;
}
li.innerHTML = li_arr;
}
< / script>
< / head>
< body>
< input type =buttononclick =go()value =Display JS Array/>
< ul id =list>< / ul>
< / body>
< / html>


I have an array.js file and an index.html.

My array.js file looks like this:

function go(){
    var array = new Array();
    array[0] = "Red";
    array[1] = "Blue";
    array[3] = "Green";
    for (var i=0; i < array.length; i++){
        document.write("<li>" + array[i] + "<br />");
    }
}

My index.html file looks like this:

<html>
    <head>
        <script type="text/javascript" src="array.js"></script>
    </head>
    <body>
        <input type="button" onclick="go()" value="Display JS Array"/>
        <script>
            go();
        </script>
    </body>
</html>

When I click the Display JS Array button on the HTML page, nothing happens. I want the array elements to be displayed after I click the button. It should look like:

  • Red
  • Blue
  • Green

解决方案

replace this document.write("<li>" + array[i] + "<br />"); with document.write("<li>" + array[i] + "</li>");

and in your HTML file, remove

<script>
  go();
</script>

because already you calling go(); function on onclick. and in your array, array[2] will give undefined.

try this its working for me :

    <html>
    <head>
        <script type="text/javascript">
            function go(){
                var array = new Array();
                array[0] = "Red";
                array[1] = "Blue";
                array[2] = "Green";
                li = document.getElementById("list");
                li_arr = "";
                for (var i=0; i < array.length; i++){
                   li_arr += "<li>" + array[i] + "</li>";
                }
               li.innerHTML = li_arr;
            }
        </script>
    </head>
    <body>
        <input type="button" onclick="go()" value="Display JS Array"/>
        <ul id="list"></ul>
    </body>
</html>

这篇关于在html中调用外部.js文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆