使用 livequery:找不到通过 ajax 创建的传播元素

发布于 2024-09-01 15:41:05 字数 598 浏览 3 评论 0原文

我正在使用 jQuery 1.3.2 和 Live Query 插件。该脚本需要在 FF 和 IE6 中运行。升级 jQuery 并使用 live 是不可能的。

不知何故,动态创建的元素不会调用此脚本。

    $('select').livequery('change',function(){
      var select_id  = $(this).attr("id");                                                                            ...
...
...

});

$('select').livequery('mouseover',hideExtensions());

function hideExtensions(){
...
...
}

在 IE6 和 FF 中,静态(已存在)元素可以正确调用该函数。但是,动态创建的元素不会调用它。

可能是什么原因?

更新 我用“live”测试了相同的功能。它在 FF 中有效,但在 IE6 中不起作用,当然不是......这就是为什么我正在寻找 livequery 的解决方法。

I'm using jQuery 1.3.2 and Live Query plugin. The script needs to work in FF as well as IE6. Upgrading jQuery and using live instead isn't a possibility.

Somehow this script won't be called by the dynamically created element.

    $('select').livequery('change',function(){
      var select_id  = $(this).attr("id");                                                                            ...
...
...

});

$('select').livequery('mouseover',hideExtensions());

function hideExtensions(){
...
...
}

In both IE6 and FF, the function is correctly called by the static (already existing) elements. However, it's not being called by the dynamically created element.

What could be the reason?

Update
I tested the same function with "live". It worked in FF, but not in IE6, of course not... That's why i'm looking for a workaround with livequery.

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

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

发布评论

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

评论(1

∝单色的世界 2024-09-08 15:41:05

是否有原因无法升级到 jQuery 1.4.2 并使用内置的 live 回调?

如果 livequery 不起作用,您可以尝试的另一种选择是使用一些核心 JavaScript 进行一些“手动”事件委托,并依赖于事件冒泡到直接父级。假设您有一个如下所示的 DOM 结构:

<div id='something'>
    <a href='#' class='some-tag'></a>
    <a href='#' class='some-tag'></a>
    <a href='#' class='some-tag'></a>
    <!-- more dynamic elements 'a' tags identical to those above added here -->
</div>

在您的 JavaScript 中:

var something = document.getElementById( 'something' );

something.addEventListener('click', function(e) {
    e = window.event || e;

    // If the target of the event is the added anchor we're looking for
    if (e['srcElement' in e ? 'srcElement' : 'target'].getAttribute('class') === 'some-tag')
        // code block goes here

    return false;

}, false);

Is there a reason you can't upgrade to jQuery 1.4.2 and use the built-in live callback there?

If livequery isn't working, an alternative you can try is some "manual" event delegation with some core JavaScript, and rely on event bubbling to an immediate parent. Let's say you had a DOM structure like this:

<div id='something'>
    <a href='#' class='some-tag'></a>
    <a href='#' class='some-tag'></a>
    <a href='#' class='some-tag'></a>
    <!-- more dynamic elements 'a' tags identical to those above added here -->
</div>

And in your JavaScript:

var something = document.getElementById( 'something' );

something.addEventListener('click', function(e) {
    e = window.event || e;

    // If the target of the event is the added anchor we're looking for
    if (e['srcElement' in e ? 'srcElement' : 'target'].getAttribute('class') === 'some-tag')
        // code block goes here

    return false;

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