IE 7 无法将事件(使用 .live())绑定到使用 .load() 动态创建的元素

发布于 2024-08-28 20:00:59 字数 1042 浏览 4 评论 0原文

我无法让 IE7 将单击事件绑定到使用 .load() 添加到 DOM 的元素。这是一些代码:

$('.mybtn').live('click', function(e){
    e.preventDefault();
    $('#mypage').load('load-this-page.htm');
});

这是 html

<div id="mypage">
   <a href="#" class="mybtn">clickme</a>
   // stuff goes here
</div>

在页面加载时,单击可以工作,但是一旦通过 clickme 链接加载了 div,单击就在 IE7 中停止工作。 clickme 链接位于加载时的 div 内,也在 load() 加载的 html 文件内,这就是我使用 live() 的原因。

仅供参考,此代码适用于 FF 3.6。

任何人都知道发生了什么(除了 IE 很糟糕这一事实)?谢谢!

编辑:这是加载到 div 中的内容

<ul>
    <li>
        <a href="02-01-2010" id="prev-month" class="mybtn"></a>
    </li>
    <li>
        <h3>March 2010</h3>
    </li>
    <li>
        <a href="04-01-2010" id="next-month" class="mybtn"></a>
    </li>
</ul>

<a href="#">link 1</a>
<a href="#">link 2</a>
<a href="#">link 3</a>
<a href="#">link 4</a>

I'm having trouble getting IE7 to keep a click event bound to an element that is added to the DOM using .load(). Here's some code:

$('.mybtn').live('click', function(e){
    e.preventDefault();
    $('#mypage').load('load-this-page.htm');
});

And here's the html

<div id="mypage">
   <a href="#" class="mybtn">clickme</a>
   // stuff goes here
</div>

On page load the click works but once the div is loaded via the clickme link the click stops working in IE7. The clickme link is within the div on load and also within the load() loaded html file that's why I'm using live().

This code works in FF 3.6, fyi.

Anyone have any idea what's up (besides the fact the IE sucks balls)? Thanks!

EDIT: here's what loads into the div

<ul>
    <li>
        <a href="02-01-2010" id="prev-month" class="mybtn"></a>
    </li>
    <li>
        <h3>March 2010</h3>
    </li>
    <li>
        <a href="04-01-2010" id="next-month" class="mybtn"></a>
    </li>
</ul>

<a href="#">link 1</a>
<a href="#">link 2</a>
<a href="#">link 3</a>
<a href="#">link 4</a>

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

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

发布评论

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

评论(4

渡你暖光 2024-09-04 20:01:01

试试这个:

var fn = function(e) {

    e.preventDefault();
    $('#mypage')
        .load('load-this-page.htm')
        .find('.mybtn')
        .click(fn);
};

$('.mybtn').live('click', fn);

Try this one:

var fn = function(e) {

    e.preventDefault();
    $('#mypage')
        .load('load-this-page.htm')
        .find('.mybtn')
        .click(fn);
};

$('.mybtn').live('click', fn);
权谋诡计 2024-09-04 20:01:00

试试这个:

$('#mypage').load('load-this-page.htm #DivOrWrapper');

Try this:

$('#mypage').load('load-this-page.htm #DivOrWrapper');
怪我太投入 2024-09-04 20:01:00

API文档中,您正在加载的页面(“load-this-page.htm” ) 不能是完整的 HTML 文档,即不能包含 </code> 或 <code><head></code> 元素,否则无法保证跨浏览器兼容性。

From the API documentation, the page you're loading ("load-this-page.htm") cannot be a full HTML document, i.e. it can't have <html>, <title> or <head> elements, otherwise cross-browser compatibility isn't guaranteed.

心如狂蝶 2024-09-04 20:01:00

这可能是由于IE浏览器的限制造成的。以下是 jQuery 文档的引用:

由于浏览器安全限制,大多数“Ajax”请求都受到 同源政策;该请求无法成功从不同的域、子域或协议检索数据。

这意味着您无法执行 $load:

Maybe this is due to IE browser restrictions. Here's a quote from the jQuery Docs:

Due to browser security restrictions, most "Ajax" requests are subject to the same origin policy; the request can not successfully retrieve data from a different domain, subdomain, or protocol.

This means that you can't do $load:

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