87,910
社区成员
发帖
与我相关
我的任务
分享
$(function(){
var $new=$('<p>sweeeeqwe<span>未改变</span></p>');
$('button').click(function(){
$('body').append($new);
})
$new.children('span').on('click',function(){
$(this).text('改变');
});
})
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<button type="button">一次</button>
<input type="button" value="多次">
<script src="http://apps.bdimg.com/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
$( function() {
/*
初始化时,创建了一个DOM元素“$new”
当点击按钮时,将 “$new” 添加到 body
*/
var $new = $( '<p>sweeeeqwe<span>1-未改变</span></p>' );
$new.children( 'span' ).on( 'click', function() {
$( this ).text( '改变' );
});
$( 'button' ).click( function() {
$( 'body' ).append( $new );
})
} )
$( function() {
/*
每次点击按钮时,创建了一个DOM元素“$new”,
给它注册事件处理程序,
并将其添加到 body
*/
$( 'input' ).click( function() {
var $new = $( '<p>sweeeeqwe<span>2-未改变</span></p>' );
$new.children( 'span' ).on( 'click', function() {
$( this ).text( '改变' );
});
$( 'body' ).append( $new );
})
} )
</script>
</body>
</html>
$(function(){
$('button').click(function(){
var $new=$('<p>sweeeeqwe<span>未改变</span></p>');
$new.children('span').on('click',function(){
$(this).text('改变');
});
$('body').append($new);
})
})
$(function(){
var $new=$('<p>sweeeeqwe<span>未改变</span></p>');
$('button').click(function(){
$('body').append($new);
$new.on('click', function(){
$(this).text('改变');
});
})
})
on 不能绑定从未出现过的节点