最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Jquery on event is not binding for non existing elements - Stack Overflow

programmeradmin4浏览0评论

Ok here the jsfiddle example

/

As you can see you when you hover it is not firing mouseover event

how can i solve this problem ?

i am using Jquery 1.9

<div id='superdiv'>Click Me</div>


$(function () {
    $('#superdiv').on('click', function (event) {
        $('body').append('<div id="super">another');
    });
    $('#super').on('mouseover', function (event) {
        alert('not working');
    });
});

javascript

Ok here the jsfiddle example

http://jsfiddle/HTjCT/1/

As you can see you when you hover it is not firing mouseover event

how can i solve this problem ?

i am using Jquery 1.9

<div id='superdiv'>Click Me</div>


$(function () {
    $('#superdiv').on('click', function (event) {
        $('body').append('<div id="super">another');
    });
    $('#super').on('mouseover', function (event) {
        alert('not working');
    });
});

javascript

Share Improve this question asked Jan 18, 2013 at 14:23 Furkan GözükaraFurkan Gözükara 23.8k81 gold badges222 silver badges363 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 14

You have to use "delegate", like this (to supply "live") $('body').on('mouseover', '#super', function (event) {

You can also wrap the mouse over event in a function, and call it whenever you add new elements that you want to be affected. That way the issue WickyNilliams pointed out won't affect you.

$(function () { 
    $('#superdiv').on('click', function (event) { 
        $('body').append('<div id="super">another');
        mouse();
    }); 
    function mouse () {
        $('#super').on('mouseover', function (event) { 
            alert('not working'); 
        }); 
    });
    mouse();
}

The div you want to create onclick, is not closed with the '< / div >' tag.

What if you try the following code:

$(function () {
    $('#superdiv').on('click', function (event) {
        $('body').append(
            $('<div/>',{ 'id' : 'super', 'text' : 'tetet'}).mouseover(function(event) { 
                    alert('test'); 
            })
        );
    });
});
发布评论

评论列表(0)

  1. 暂无评论