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');
});
精彩评论