使用 Ajax 加载页面内容后加载 Javascript

发布于 2024-12-10 10:54:19 字数 1101 浏览 0 评论 0原文

我正在使用 Ajax(以及 jQuery,还有 CoffeeScript)在我正在创建的新网站中加载页面。对于导航菜单,我使用 jQuery 函数 load 仅加载一个页面的一部分(即内容)如下(来自 /page1):

$( 'a' ).click ->
  $( '#container' ).load '/page2 #content'

  false

问题是我在 page1 中有 JavaScript 代码(或者更正确地说,在模板文件中)在站点上是全局的(由于资产管道因为我使用的是 Rails 3.1)。此代码将一些操作绑定到位于 page2 中的 DOM 元素中的事件(这是正确的术语吗?)。但是,由于此脚本在首次加载的页面(很容易是 page1)中运行,因此将这些操作绑定到元素的脚本将在 page1 中执行>,因此它们实际上应该绑定到的 DOM 元素(在 page2 中)将不会被触及。

现在,我知道一些可以解决这个问题的方法,但我都不喜欢它们。其中之一是将此操作/事件绑定放入一个函数中,然后在使用 Ajax 加载 page2 后调用此方法,但这需要我在页面加载中引入特殊情况我在页面加载后加载绑定函数以绑定元素的逻辑,或者在我调用该函数的 page2 内容中包含一个脚本。

我宁愿不采用前一种方法,因为我有一种更通用的加载页面的方法(通过使用 标记的 href 属性),并引入特殊的页面加载的情况只会丑化这一切。我相信后一种解决方案是一个相当好的解决方案,但我不想将

所以,我真正想知道的是解决这个问题的好方法。如果有人知道如何去做这件事,那么我欢迎任何建议。

I am using Ajax (and jQuery, and also CoffeeScript) to load pages in a new web-site I am creating. For the navigation menu I then use the jQuery function load to load only one part of the page (namely, the content) like this (from /page1):

$( 'a' ).click ->
  $( '#container' ).load '/page2 #content'

  false

The problem with this is that I have JavaScript code in page1 (or, more correctly, in a template file) that is global on the site (due to the asset pipeline as I am using Rails 3.1). This code binds some actions to events (is this the correct jargon?) in DOM elements that are located in page2. But, as this script is run in the page that is first loaded (which easily could be page1), then the script that binds these actions to the elements will be executed in page1, and thus the DOM elements that they actually should be bound to (in page2) will not be touched.

Now, I know some ways I can fix this, but I don't like neither of them. One of these would be to put this action/event binding into a function, and then call this method after loading page2 with Ajax, but this would require me to either have introduce a special case in my page loading logic where I load the binding function after page load to bind the elements, or to have a script in the page2 content where I call the function.

I would rather not go with the former approach, as I have a more general way of loading pages (by using the <a> tag's href attribute), and introducing special cases on page load just uglifies it all. The latter solution is a rather nicer one I believe, but I would hate to put <script>tags into the content of the page that I am retrieving. I like separating content from behaviour and all that, and I feel that this might be mixing it all up a bit.

So, what I really want to know here is whatever would be a good way to solve this problem. If anyone out there knows how to go about doing this, then I would welcome any suggestions.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

回心转意 2024-12-17 10:54:19

您可能想研究一下 jQuery 的 live() 事件绑定。

live() 绑定的事件也适用于追溯添加的 DOM 元素,这应该可以解决您的问题。

编辑:准确地说,live()绑定到$(document)并检查事件的target属性它们一直在 DOM 中向上冒泡——这就是为什么即使在绑定事件回调后添加元素,绑定仍然有效。

HTML 示例:

<div id="some_triggers">
    <a id="foo" href="#">Foo!</a>
</div>

jQuery 示例:

$('#foo').click(function(){ alert('foo') });
$('#bar').click(function(){ alert('bar') });
$('#some_triggers').append('<a id="bar" href="#">Bar!</a>');
$('#foo').live('click', function(){ alert('foo live()!') });
$('#bar').live('click', function(){ alert('bar live()!') });

// -> clicking #foo alerts "foo" and "foo live()"
// -> clicking #bar alerts "bar live()" only, since the '.click()' binding doesn't work on non-existent DOM elements.

You might want to look into jQuery's live() event binding.

Events bound with live() are applied to retrospectively added DOM elements as well, which should solve your problem.

Edit: To be precise, live() binds to $(document) and checks the target attribute of events that have bubbled all the way up through the DOM – that's why the binding works even when elements are added after binding the event callback.

Example HTML:

<div id="some_triggers">
    <a id="foo" href="#">Foo!</a>
</div>

Example jQuery:

$('#foo').click(function(){ alert('foo') });
$('#bar').click(function(){ alert('bar') });
$('#some_triggers').append('<a id="bar" href="#">Bar!</a>');
$('#foo').live('click', function(){ alert('foo live()!') });
$('#bar').live('click', function(){ alert('bar live()!') });

// -> clicking #foo alerts "foo" and "foo live()"
// -> clicking #bar alerts "bar live()" only, since the '.click()' binding doesn't work on non-existent DOM elements.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文