Facebox 灯箱内的 Javascript

发布于 2024-10-02 07:49:17 字数 917 浏览 0 评论 0原文

我正在开发一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北风几吹夏 2024-10-09 07:49:17

我认为在您的 document.ready 代码中您正在执行类似的操作,

$(".supercool")..bind('click', function() {
  somethingAwesome();
});

因此它将附加到加载时存在的具有 supercool 类的所有类。你想做的是使用 live

$('.supercool').live('click', function() {
  // Live handler called.
});

这只是一个猜测——我认为这就是正在发生的事情——如果不是这样的话,需要看到更多的 javascript。

http://api.jquery.com/live/

I think in your document.ready code you're doing something like

$(".supercool")..bind('click', function() {
  somethingAwesome();
});

So that will attach to all classes with supercool class that exist on load. What you want to do is use the live

$('.supercool').live('click', function() {
  // Live handler called.
});

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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文