87,992
社区成员
发帖
与我相关
我的任务
分享
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type='text/javascript'>
$(document).ready(function(){
$('#button').click(function(){
$('body').append('<div class="future">I am a new div</div>');
});
$('div.future').live('click',function(){
$(this).css({'color':'red','font-weight':'bold'}).html('You clicked me'); //想实现单击新增加的div后,会改变div的内容和字体
});
});
</script>
$('body').on('click',"div",function(){
$(this).css({'color':'red','font-weight':'bold'}).html('You clicked me');
});
函数原型:
.on( events [, selector ] [, data ], handler(eventObject) )
jquery API on
不要再听到有人说live了!!!<html>
<head>
<title>New Document </title>
<meta name="Generator" content="EditPlus">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<style type='text/css'>
div
{
border: 1px solid black;
cursor: pointer;
width: 200px;
margin: 10px;
}
</style>
</head>
<body>
<input type='button' id='button' value='Create New Element' />
<div class='future'>
Already on page</div>
<script type='text/javascript' src='jquery-1.10.2.min.js'></script>
<script type='text/javascript'>
$(function () {
$('#button').click(function () {
$('body').append('<div class="future">I am a new div</div>');
});
$('body').on('click', "div",function () {
$(this).css({ 'color': 'red', 'font-weight': 'bold' }).html('You clicked me'); //想实现单击新增加的div后,会改变div的内容和字体
});
});
</script>
</body>
</html>
$(document).on('click','div', function() {
$(this).css({
'color' : 'red',
'font-weight' : 'bold'
}).html('You clicked me'); //想实现单击新增加的div后,会改变div的内容和字体
});
都说不要再提live了,都被废除了,打入冷宫的东西,还一口一口娘娘叫的。
LZ我的代码试了没?[/quote]
代码试过了,也没效果
都说不要再提live了,都被废除了,打入冷宫的东西,还一口一口娘娘叫的。
LZ我的代码试了没?