当元素完全加载时调用 JavaScript 函数
我在 ModalWindow(来自 wicket 扩展)上有以下代码(在 html 文件中):
<wicket:head>
<script src="static/js/scroll.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
ffscroll('.scroller2');
});
</script>
</wicket:head>
<wicket:panel>
<div wicket:id="scroller2" id="scroller2" style="overflow-x: hidden; overflow-y: auto; height: 240px" class="scroller2 YYYY ZZZZ">
....
</div>
</wicket:panel>
和scroll.js
function ffscroll(id) {
$(id).scroll(function () {
alert(id);
});
};
如果我使用简单的页面,则此代码工作正常:每次滚动时我都会收到警报。
但是在 ModalWindow 中我看到 JavaScript 代码是在模态窗口显示之前执行的,所以我想我需要在模态窗口显示之后调用 ffscroll('.scroller2'); ,但我不这样做不知道该怎么做。
奇怪的是,在 firebug 中我的脚本显示如下:
function () {
ffscroll(".scroller2");
alert("aaadssd");
}
“而不是 '.
I have on a ModalWindow (from wicket extensions) the following code (in html file):
<wicket:head>
<script src="static/js/scroll.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
ffscroll('.scroller2');
});
</script>
</wicket:head>
<wicket:panel>
<div wicket:id="scroller2" id="scroller2" style="overflow-x: hidden; overflow-y: auto; height: 240px" class="scroller2 YYYY ZZZZ">
....
</div>
</wicket:panel>
and scroll.js
function ffscroll(id) {
$(id).scroll(function () {
alert(id);
});
};
If I use a simple page this code works fine: every time I scroll I receive an alert.
But in ModalWindow I saw that the JavaScript code is executed before the modal window is displayed, so I suppose I need to call ffscroll('.scroller2'); after the modal window is displayed, but I don't know how to do this.
A strange thing is that in firebug my script is displayed like this:
function () {
ffscroll(".scroller2");
alert("aaadssd");
}
" instead of '.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最好的做法是将 JS 放入资源文件中,然后将其添加到您正在使用的任何组件中:
对于全局定义文件:
对于相对包:
由于
ModalWindow
是使用 ajax 加载的,因此其所需的 JavaScript 源文件将被下载并在 ajax 回调上运行。或者,如果您查看
ModalWindow.show(final AjaxRequestTarget target)
,它会使用target.appendJavascript()
将 JavaScript 添加到AjaxRequestTarget
,您可以覆盖show
方法添加额外的 JavaScript:然后,当在客户端浏览器上调用 Ajax 回调时,该 JavaScript 将运行。
希望有帮助
It's good practice to put your JS in a resource file, then add it in which ever component you are using:
For globally define files:
For package relative:
Since
ModalWindow
are loaded using ajax its required JavaScript source files will be downloaded and run on the ajax callback.Or if you look at
ModalWindow.show(final AjaxRequestTarget target)
it adds JavaScript to theAjaxRequestTarget
usingtarget.appendJavascript()
, you could override theshow
method to add the extra JavaScript:Then when the Ajax callback is called on the client browser this JavaScript will get run.
Hope that helps