Facebox 灯箱内的 Javascript
我正在开发一个 Rails 应用程序,其中使用 jQuery 和 Facebox Lightbox 作为弹出表单。我想在表单上使用一些 jQuery 验证。当我尝试实现这个时,我遇到了一些奇怪的结果。
如果我将 javascript 直接放入我在弹出窗口中加载的视图中,则 javsacript 工作得很好。
示例:app/views/item/view.html.erb
...
<script type="text/javascript">
$(document).ready(function() {
...
});
</script>
但是,如果我将其抽象一步并将 JavaScript 放入我用于弹出窗口的布局中,则会失败。
示例:app/views/layouts/popup.html.erb
<script type="text/javascript">
$(document).ready(function() {
...
});
</script>
<div id="popup">
<%= yield %>
</div>
如果我将直接编码到视图中的脚本拉入单独的 javascript 文件中,例如,如果我将相同的脚本放入 ../public 中,它也会失败/javascripts/popup.js
然后在我看来我这样做:
<%= javascript_include_tag 'popup' %>
它在这里失败了。
您可能会认为它位于哪个文件中没有任何区别。它应该以相同的方式加载。
知道为什么会发生这种情况吗?
I am working on a rails app, where I am using jQuery and the Facebox Lightbox for popup forms. I want to use some jQuery validation on the form. When I try to implement this, I run into some weird results.
If i put the javascript directly into the view I am loading in the popup, the javsacript works just fine.
Example: app/views/item/view.html.erb
...
<script type="text/javascript">
$(document).ready(function() {
...
});
</script>
However, if I abstract it one step and put the javascript into the layout I am using for the popup, it fails.
Example: app/views/layouts/popup.html.erb
<script type="text/javascript">
$(document).ready(function() {
...
});
</script>
<div id="popup">
<%= yield %>
</div>
It also fails if I pull the script from being directly coded into the view into a seperate javascript file, for example if i put the same script into ../public/javascripts/popup.js
then in my view i do:
<%= javascript_include_tag 'popup' %>
It fails here to.
You'd think that it wouldn't make any difference which file it's in. It should get loaded the same way.
Any idea on why this is happening?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为在您的 document.ready 代码中您正在执行类似的操作,
因此它将附加到加载时存在的具有 supercool 类的所有类。你想做的是使用 live
这只是一个猜测——我认为这就是正在发生的事情——如果不是这样的话,需要看到更多的 javascript。
http://api.jquery.com/live/
I think in your document.ready code you're doing something like
So that will attach to all classes with supercool class that exist on load. What you want to do is use the live
That's just a guess though -- I think that's what's going on -- need to see more javascript if it's not that.
http://api.jquery.com/live/