【问题标题】:Javascript placement of an external file外部文件的 Javascript 放置
【发布时间】:2014-07-22 21:30:13
【问题描述】:

我有一个 HTMl 文档,它是单选按钮,以及一个位于外部文件 filename.js 中的函数 我正在使用 inside 。 它在所有可能的方面都是正确的,从书中复制猫,但是当它不起作用时 执行html文件。我已将 .html 文件和 filename.js 文件放入新文件夹中,其中只有两个文件。这里似乎有什么问题,我喜欢一些建议。

作者x。

【问题讨论】:

  • 您也可以发布一些代码吗?
  • 请发布一些代码示例

标签: javascript file external


【解决方案1】:

如果没有看到一些代码,我不能确切地说,但是从 html 文件执行单独的 javascript 文件的标准方法是使用 script 标签。更多信息在这里:http://www.w3schools.com/tags/tag_script.asp

在你的情况下,标签看起来像

<script src="filename.js"></script> 

【讨论】:

    【解决方案2】:

    我的代码是这样的

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="utf-8" />
    <title> Radio Button </title>
    <script type= "text/javascript" src = "radio_plane.js" >
    </script>
    
    <body>
    <h4> Cessna single-engine airplane description </h4>
    <form id="myform" action="">
    <p>
    <label><input type="radio" name="planebutton" value="152" onclick="action(152);" />
                    Model 152 </label>
                    <br />
    <label><input type="radio" name="planebutton" value="172" onclick="action(172);" />
                    Model 172 (Skyhawk)</label>
                <br />
    <label><input type="radio" name="planebutton" value="182" onclick="action(182);" />
                    Model 182</label>
                <br />
    <label><input type="radio" name="planebutton" value="210" onclick="action(210);" />
                Model 210 (Centurian) </label>
                <br />
    </p>
    </form>
    
    </body>
    </html>
    and javascript file
    
    
    function action(plane)
    {
    switch(plane)
    {
    case 152:
    alert("A small two-place airplane for flight training");
    break;
    case 172:
    alert("The Smaller of two four-plane airplanes");
    break;
    case 182:
    alert("The larger of two four-place airplanes");
    break;
    case 210:
    alert("A six-place high-performance airplane");
    break;
    default:
    alert("Error in JavaScript function planeChoice");
    break;
     }
     }
    

    【讨论】:

    • 代码示例应该在您的原始问题中,而不是在单独的答案中。考虑编辑您的原始问题以包含此代码。
    猜你喜欢
    • 2021-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-11
    • 2016-05-15
    • 1970-01-01
    • 2011-07-11
    相关资源
    最近更新 更多