gpt4 book ai didi

jquery单独的js文件

转载 作者:行者123 更新时间:2023-12-01 03:06:11 25 4
gpt4 key购买 nike

我有 2 个独立的 js 文件:


//first.js
$(document).ready(function(){

$("#someButton").click(function(event){

showMessageBox("test");
});
});

第二个


$(document).ready(function(){

alert('csdfsdf');

function showMessageBox(msg) {
alert("something");
$('#messageBox').text(msg);
$('#messageBox').dialog('open');
}


$("#messageBox").dialog({
bgiframe: true,
autoOpen: false,
height: 200,
width: 300,
modal: true,
buttons: {
'Ok': function() {
$(this).dialog('close');
window.location.reload(true);
}
}
});


});

第二个文件中的 showMessage() 函数我想在其他 jquery 风格的 js 文件中使用它,就像第一个文件一样。问题是当我尝试使用 showMessage() 时收到一条错误消息,提示该错误未定义。

我调用的每个其他函数都发生这种情况,这些函数位于单独的 jquery 样式文件中。如果 showMessage() 函数不在 $(document).ready(function(){.....});然后它被调用,但是它无法与 jquery 一起使用。

我还确保包含 showMessage() 函数的文件在调用它的文件之前加载。

如有任何帮助,我们将不胜感激。

谢谢。

最佳答案

据我所知,您在调用中使用了错误的函数名称,声明的函数是 showMessageBox 并且您正在调用 showMessage。此外,您需要在 document.ready block 之外声明该函数,以便它在全局上下文中可用。

从一个 document.ready block 声明的函数无法从其他 document.ready block 访问,我之前已经执行过该测试。请参阅这个答案:

Can you have multiple $(document).ready(function(){ ... }); sections?

关于jquery单独的js文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1393745/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com