带外部js文件的警报消息

时间:2012-01-02 14:52:06

标签: jquery

我有一个带有代码的外部test.js文件:

$(document).ready(function() {
    alert("I am an alert box2!");   
});


function show_alert()
{
    alert("hihi");
}

在我的.html中,我已经包含了test.js,jquery文件,还有一个按钮。单击它时,将调用show_alert()函数并且它可以正常工作。但是,消息“我是一个警报框2!”不被解雇。
另外,如果我将document.ready函数放在带有<script>... </script>的.html中。有用。我的代码有什么问题?外部文件不支持?

3 个答案:

答案 0 :(得分:7)

以下其中一项:

  1. 你已经在你头脑中的jquery.js之前加入了test.js.
  2. 您有一个错误,它会在您的javascript到达就绪函数之前停止它。

答案 1 :(得分:2)

它对我有用,

这里是代码:

function show_alert()
{
    alert("hihi");
}

$(document).ready(function() {
    alert("I am an alert box2!");

    $('.button').click(function() {
        show_alert();
    });
});

将jQuery.min.js放在HTML代码中的test.js之前。

答案 2 :(得分:1)

您可以将testAlert函数保存在test.js.但是,document.ready应该只在脚本标签之间。

function testAlert(){
alert("I am an alert box2!");
}

以上功能可以在test.js。

$(document).ready(testAlert);应该在当前页面的脚本标记中以检查DOM的可用性。

Acutally,Above不是强制性的,而是编写jquery的理想方式,因为它有助于轻松调试:)

相关问题