对于通过ajax注入的html,重新运行 document.ready() jqueries,可以吗?

发布于 2024-10-08 11:04:17 字数 783 浏览 4 评论 0原文

here is my case:

    <html>
    <body>
    <head>
    ...
    <script>
 $(function(){
        $('.do-tip').qtip({
            content: 'This is a tip of active hovered element',
            show: 'mouseover',
            hide: 'mouseout',
  })
 });
    </script>
    </head>
    <body>
    <a href="http://www.google.com" class="do-tip">google</a>

    <input type="button" value="load div by ajax" />
    <div> <!-- this div loaded by ajax -->
    <div>
    <a href="http://www.yahoo.com" class="do-tip">yahoo</a> <!-- HOW TO HERE, run the existing script only for this part, JQUERY UNCLE must have a solution-->
    </body>
    </html>

any ideas?
here is my case:

    <html>
    <body>
    <head>
    ...
    <script>
 $(function(){
        $('.do-tip').qtip({
            content: 'This is a tip of active hovered element',
            show: 'mouseover',
            hide: 'mouseout',
  })
 });
    </script>
    </head>
    <body>
    <a href="http://www.google.com" class="do-tip">google</a>

    <input type="button" value="load div by ajax" />
    <div> <!-- this div loaded by ajax -->
    <div>
    <a href="http://www.yahoo.com" class="do-tip">yahoo</a> <!-- HOW TO HERE, run the existing script only for this part, JQUERY UNCLE must have a solution-->
    </body>
    </html>

any ideas?

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

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

发布评论

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

评论(3

李白 2024-10-15 11:04:17

虽然尚不完全清楚您想要做什么,但我会尝试我的 MagicGuess 工具并为您提供答案。

因此,您需要

a) 在文档加载上执行一些功能;此功能对页面上的所有 .do-tip 元素执行某些操作

b) 在通过 AJAX 加载某些内容后执行相同的功能,但现在需要与另一组元素一起操作。

这是真的吗?如果是这样,我会这样做:

function doEverythingINeed(selector) {
    $(selector).qtip({
        content: 'This is a tip of active hovered element',
        show: 'mouseover',
        hide: 'mouseout',
    })

}

$(function() {
    doEverythingINeed('.do-tip');
});

在通过 AJAX 加载 HTML 后,使用另一个选择器调用 doEverythingINeed 。如果您使用 $.ajax() 函数,那么您应该这样做

$.ajax({
    //parameters
    success: function() {
        //some other actions if needed
        doEverythingINeed('your selector');
        //more actions if needed
    }
});

希望有帮助!

While it is not perfectly clear what you are trying to do, I will try my MagicGuess tool and offer you an answer.

So, you need to

a) perform some functionality on document load; this functionality does something with all .do-tip elements on the page

b) perform the same functionality after you load something via AJAX, but now this needs to operate with another set of elements.

Is that true? If so, here is what I would do:

function doEverythingINeed(selector) {
    $(selector).qtip({
        content: 'This is a tip of active hovered element',
        show: 'mouseover',
        hide: 'mouseout',
    })

}

$(function() {
    doEverythingINeed('.do-tip');
});

and call doEverythingINeed with another selector after you load your HTML via AJAX. If you use $.ajax() function, then you should just do

$.ajax({
    //parameters
    success: function() {
        //some other actions if needed
        doEverythingINeed('your selector');
        //more actions if needed
    }
});

Hope that helps!

平定天下 2024-10-15 11:04:17

为了防止它对其他人有帮助,我有一个非常相似的用例,并使用 jQuery 的 .live() 事件绑定器解决了它 - 比我试图通过重新处理所有 $('document').ready 来设计的解决方案简单得多() 事件。

用例:一个页面上有很多链接(HTML a 标签)。在 $('document').ready() 上,我将相关链接的 .click() 事件绑定到一个调用某些 .ajax() 的函数,该函数检索更多相同类型的链接并将其 HTML 注入到页面中。问题是,新链接没有绑定。单击新链接不会像应有的那样触发 .ajax() 代码。

我花了很长时间尝试在注入 HTML 后重新触发 read 事件,然后意识到使用 .live('click) 而不是 .click() 绑定事件将给出完全相同的结果,且额外代码为零。

顺便说一句,其他人在其他地方为原始问题提供了非常好的解决方案 - 请参阅问题 2238030

Just in case it helps someone else, I had a very similar use case and solved it with jQuery's .live() event binder - much simpler than the solution I was trying to engineer by re-processing all $('document').ready() events.

The use case: A page with a lot of links (HTML a tags) on it. On $('document').ready() I bound the .click() events of the relevant links to a function that called some .ajax(), which retrieved more links of the same type and injected their HTML into the page. Problem was, the new links weren't bound. Clicking on the new links didn't trigger the .ajax() code like it should.

I spent ages trying to re-trigger the ready event after the HTML was injected and then realised that binding the events with .live('click) instead of .click() would give exactly the same result with zero extra code.

By the way, someone else has given a very good solution to the original question elsewhere - see question 2238030

野心澎湃 2024-10-15 11:04:17

您可以在就绪函数中构建模块执行控制器,并在注入 HTML 后重新运行这些函数。像这样的东西:

https://gist.github.com/miguel-perez/476046a42d229251fec3

You can build in a module execution controller into the ready function and re-run the functions after you've injected the HTML. Something like this:

https://gist.github.com/miguel-perez/476046a42d229251fec3

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