这是你想做的吗?注意,我将
$.on()
放在父元素上,但选择了
.button
作为动作对象。
.on( events[, selector] [, data], handler(eventObject) )
selector:选择器字符串,用于过滤触发事件的所选元素的后代。如果选择器为空或省略,则事件总是在达到所选元素时触发。
http://api.jquery.com/on/
<div id="stuff">
<button class="button">Click me!</button>
<p>Stuff</p>
</div>
var $stuff = $('#stuff'),
ajaxContent = $stuff.html();
$stuff.on('click', '.button', function(){
$.get('/echo/html/', function(){
$stuff.empty();
console.log($stuff.html());
alert($stuff.html());
$stuff.html(ajaxContent);
console.log($stuff.html());
});
});
这里是另一个演示
下面是需要翻译的内容:
var $stuff = $('#stuff'),
ajaxContent = $stuff.html(),
$ajaxContent,
colors = ['blue','green','red'],
color = 0;
$stuff.on('click', '.button', function(){
$.get('/echo/html/', function(){
color++;
if (color == colors.length) color = 0;
console.log($stuff.html());
alert($stuff.html());
$ajaxContent = $(ajaxContent);
$stuff.append($ajaxContent).css('color', colors[color]);
console.log($stuff.html());
});
});
http://jsfiddle.net/62uSU/2/
live
应该处理的内容,所以奇怪的是它没有起作用。 - Brandan