本文介绍了在Shadowbox内/内调用Jquery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的Shadowbox,由jquery调用:

I have a simple Shadowbox that is called by jquery:

$('#statsButton').click(function() {
    thisContent = $('#userStats').html();
    Shadowbox.open({
        content: thisContent,
        player: "html"
    });
});

我有一个div,该$('#userStats')对象应在单击时发出警报:

I have a div, in that $('#userStats') object that should alert on click:

$("#submitPosition").click(function(e) {
        alert('test');
});

但是,当我单击时,什么也没有发生.以上两个功能都包装在它们自己的文档就绪块中.我在脚本的开头调用了Shadowbox.init,效果很好.只是它忽略了内部的jQuery事件.有人可以帮忙吗?

But when I click, nothing happens. Both of the above functions are wrapped in their own document ready blocks. I call Shadowbox.init at the beginning of the script and it works great. It's just that it ignores jQuery events inside. Can anybody help?

简短回顾:基本上,我有Shadowbox和HTML播放器.我希望jQuery在该HTML播放器中按预期方式运行.

Short recap: Basically I have Shadowbox bring up and HTML player. I want jQuery to act as expected within that HTML player.

推荐答案

如果将内容动态加载到容器中,则应使用jQuery on()将事件绑定到内容内的对象,因为使用click()仅会绑定该事件到现有的#submitPosition.

If you are loading content dynamically into a container, you should use jQuery on() to bind events to objects inside the content because using click() will only bind the event to the existing #submitPosition.

例如:

$('body').on('click', '#submitPosition', function(e) {
   alert('test');
});

此外,在页面上使用具有相同ID的多个元素也不是一个好习惯.

Also, using multiple elements on a page with the same id is not a good practice.

http://api.jquery.com/on/

这篇关于在Shadowbox内/内调用Jquery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 12:08