开发者

Reload only jquery in page

开发者 https://www.devze.com 2023-03-17 20:49 出处:网络
I use jQuery and XAJAX at my page. I have for example: <span id=\"one\">one</span> <span id=\"two\">two</span>

I use jQuery and XAJAX at my page. I have for example:

<span id="one">one</span>
<span id="two">two</span>

The page is successfully loaded and then I add an element with XAJAX:

<span id="one">one</span>
<span id="two">two</span>
<span id="three">three</span>

There is no page reload. But then the problem happends, and I can't click the new added item:

$(document).ready(function() {
        $("#one").click(function() {
           alert('one');
    }); // This works

        $("#three").click(function() {
           alert('one');
    }); // This doesn't working

});
开发者_JAVA技巧

How can I make it work, so I can click the added element?


If you're adding things use the live method:

$("#three").live('click', function(){
    alert('three');
});


You should use live ().

live() will apply to dynamically added objects.

$("#three").live('click', function(){
    alert('three');
});

http://api.jquery.com/live/


(ps.: i suggest you to create a .class to create a single .live()... otherwise you'll have to create a new live() for each new item...)

some like:

$(".numbers").live('click', function() {
    alert('one');
});

:)


Your question is very confusing. But I guess you just want live events:

$("#one").live('click', function() {
    alert('one');
});

$("#three").live('click', function() {
    alert('one');
});
0

精彩评论

暂无评论...
验证码 换一张
取 消