Calling Jquery within / inside a Shadowbox

倾然丶 夕夏残阳落幕 提交于 2020-01-03 04:35:08

问题


I have a simple Shadowbox that is called by jquery:

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

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

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

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?

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


回答1:


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.

For instance:

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

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

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




回答2:


Try this:

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

This will help. Bcoz when this code intialise time the #submitPosition is not there in html. But live will solve this issue.




回答3:


I see 2 options:

1) In your userStat div, change to:

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

which will actually call your callback function.

2) Use the onOpen event of shadowbox:

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


来源:https://stackoverflow.com/questions/13182875/calling-jquery-within-inside-a-shadowbox

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!