外部 javascript 不工作,但在头脑中工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14131287/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 20:47:16  来源:igfitidea点击:

External javascript not working but works in head

javascripthtmlexternal

提问by user1944305

I'm new to this but I'm having trouble getting my external js to work (however it works in the head of HTML doc), I've looked at all solutions on here already.

我是新手,但我无法让我的外部 js 工作(但是它在 HTML 文档的头部工作),我已经在这里查看了所有解决方案。

This is my HTML, the js src is script2.js

这是我的 HTML,js src 是 script2.js

<!DOCTYPE html >
<html>
<head>

<link href="css/stylesheetelaine.css" rel="stylesheet" type="text/css"/> 


<script type="text/javascript" src="js/script2.js"> </script>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.3/jquery.min.js"></script>

<script src="js/jQuery/jquery.monte.js"></script>     

<meta charset="utf-8">
<title>Gallery</title>


</head>

<body>
<div id="wrapper">

<div id="title">

<h1><a href="index.html"><img src="img/elaine4.png"  width="517" height="185"    alt="elaine cullinan"></a><img src="img/elainefilligree.png" width="235" height="251" alt="elaine"></h1>


<div id="nav">
<ul id>

<li><a href="about.html" title="About">About</a></li>
<li><a href="tattoo.html" title="Tattoo">Tattoo</a></li>
<li><a href="makeup.html" title="Make Up">Make Up</a></li>
<li><a href="gallery.html" title="Gallery">Gallery</a></li>
<li><a href="contact.html" title="Contact">Contact</a></li>

</ul>
</div>

<div id="gallerywrap">

    <div id='example1' class='container'>
        <img src="img/slide/img1.png" alt='An eastern mud turtle hatching.'/>
        <img src="img/slide/img1.png" alt='Just hatched.'/>
        <img src="img/slide/img1.png" alt='After three months.'/>
        <img src="img/slide/img1.png" alt='Taking an occassional bask.'/>
        <img src="img/slide/img1.png" alt='Ornery but healthy at two and a half years.'/>
    </div>




</div>
</body>

</html>

My external js:

我的外部js:

        $(function () {
            // Unstyled Example
            $.monte('#example1');


            // Styled Buttons Example
            // (see the CSS in the above style block)
            $.monte('#example2', {auto:false});


            // Callback Example
            // Format and append the HTML:
            $('#example3 > img').each(function(){
                $(this)
                .wrap('<div style="position:relative"/>')
                .parent()
                .append('<div><p>' + $(this).attr('alt') + '</p></div>')
                .append('<img src="frame.png" alt="" class="frame"/>');
            });
            // Hide the text on all but the center slide:
            $('#example3 div div').css({opacity: 0}).eq(0).css({opacity: 0.8});
            // Using the callbacks to reveal and hide the text:
            $.monte('#example3', {
                auto:false,
                callbackIn: function () {
                    $(this[0]).find('div').animate({opacity: 0.8}, 450);
                }, 
                callbackAway: function () {
                    $(this[0]).find('div').animate({opacity: 0}, 450);
                }
            });
        });

回答by John Conde

You're loading beforeyou load jQuery. You need to load it after.

您在加载jQuery之前正在加载。您需要在之后加载它。

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.3/jquery.min.js"></script>
<script type="text/javascript" src="js/script2.js"> </script>

回答by nnnnnn

Move this line:

移动这一行:

<script type="text/javascript" src="js/script2.js"> </script>

To afterwhere you include jquery.min.jsand jquery.monte.js.

为了之后,你有jquery.min.jsjquery.monte.js

Scripts are executed in order, so you need this:

脚本按顺序执行,因此您需要:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.3/jquery.min.js"></script>
<script src="js/jQuery/jquery.monte.js"></script>
<script type="text/javascript" src="js/script2.js"></script>

So that the browser first loads jQuery, then loads the Monte plugin, then does your script.

这样浏览器首先加载 jQuery,然后加载 Monte 插件,然后执行您的脚本。

回答by Murali

To run the external javascript. Declare it at the bottom after tag. It will work.

运行外部javascript。在标签后的底部声明它。它会起作用。

</body><script src="js/validate.js"></script></html>

回答by user8373425

make sure that these points were not forgotten

确保没有忘记这些要点

  1. Add both js file and html file in single directory sometimes keeping both file in single drive also gives error

  2. Dot extension of file (external.js) line should look like this <script type="text/javascriipt" src="external.js">even if the source address is changed, the error occurs.

  1. 在单个目录中同时添加 js 文件和 html 文件有时将两个文件保存在单个驱动器中也会出错

  2. 文件(external.js)行的点扩展应该是这样的, <script type="text/javascriipt" src="external.js">即使源地址改变了,也会发生错误。