在哪里放我的$(文件).ready(function(){当我在.js中有方法时 [英] Where to put my $(document).ready(function(){ When I have methods in a .js
问题描述
我创建了somefile.js,其中somefile.js包含一些jQuery方法。所以例如它包含这个函数:
I created a somefile.js where somefile.js contains some jQuery method. So for instance it contains this function:
function showWindow(divID)
{
var dialogDiv = $(divID);
dialogDiv.dialog
(
{
bgiframe: true,
modal: true,
autoOpen: false,
show: 'blind'
}
)
dialogDiv.dialog("open");
}
所以在我的.aspx页面(或者其他什么,它可能是.html ),我有一个按钮:
So in my .aspx page (or whatever, it could be an .html), I have a button:
<input type="button" onclick="showPopUp('testDiv')" value="Click Me!" />
我的问题是,我们将在我们的应用程序中全部使用showPopUp。如果它是从onClick事件中调用的,那么我在哪里放置我的$(document).ready(function(),因为这段代码不在页面中,而是在.js文件中?
My question is, we're going to use showPopUp all over in our app. If it's called from an onClick event, then where do I put my $(document).ready(function() since this code is not in the page, but in a .js file?
推荐答案
如果你愿意,可以将它放在Javascript文件中。如果你用这样的onclick属性附加监听器,你实际上并不需要使用$(文件).ready()。
You can put it in the Javascript file if you want. If you're attaching listeners with the onclick attribute like that, you don't actually need to use $(document).ready().
然而,通常认为不使用onclick属性更好的形式,并在Javascript中附加监听器,如下所示:
However, it's generally considered better form to not use the onclick attribute, and attach the listener in Javascript, like so:
< input type =buttonclass =showPopupid =testDivvalue =Click Me!/>
$(document).ready(function(){
$("input.showPopup").click(function(){
showWindow($(this).id);
}
function showWindow(id){...}
}
你可以把这个javascript放在文件的头部,比如〜ricebowl说。
You can put that javascript in the head of the document like ~ricebowl said.
这篇关于在哪里放我的$(文件).ready(function(){当我在.js中有方法时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!