如何向动态创建的div添加诸如隐藏之类的事件?
php生成下面的html代码,然后我将其附加到页面中。之后,我可以点击delete li删除整个文章div或隐藏它。
我试过了,但没有起作用。
$( '.artical[id='+id+']' ).hide( "slow", function() {
$('.artical[id='+id+']').remove();
});
?>
<div class="artical">
<h1><?php echo $obj['title']; ?></h1>
<ul class="articalInfo">
<li> February 12, 2014, 0</li>
<li>Comments</li>
<li>More</li>
<li class="delete" name="<?php echo $id;?>">Delete</li>
<li class="edit" name="<?php echo $id;?>">Edit</li>
</ul>
<p>
<?php echo $obj['text'];
break;
?>
</p>
</div>
<?php
$( "body" ).delegate( "li", "click", function() {
// send the id to removefunction to remove the element
var r=confirm("Do you want to remove this artical?");
if (r==true)
{
removeArticle($(this).attr('name'));
}
});发布于 2014-03-28 05:03:53
尝尝这个,
$(document).on( "click","li",function() {
// send the id to removefunction to remove the element
if (confirm("Do you want to remove this artical?")) {
removeArticle($(this).attr('name'));
}
});而且你发布的code是错误的,试试这个,
<div class="artical">
<h1><?php echo $obj['title']; ?></h1>
<ul class="articalInfo">
<li> February 12, 2014, 0</li>
<li>Comments</li>
<li>More</li>
<li class="delete" name="<?php echo $id;?>">Delete</li>
<li class="edit" name="<?php echo $id;?>">Edit</li>
</ul>
<p>
<?php echo $obj['text'];
break;// don't use break if it has no loops and conditions
?>
</p>
</div>
<script>
$(function(){
$(document).on( "click","li",function() {
// code for removing article after confirmation
if (confirm("Do you want to remove this artical?")) {
$('#'+$(this).attr('name')).hide( "slow", function() { // hiding the article
$(this).remove(); // remove the article
});
}
});
});
</script>发布于 2014-03-28 05:02:14
使用在……上面 JQuery函数
$(document).on("click", "li", function () {
// send the id to removefunction to remove the element
var r=confirm("Do you want to remove this artical?");
if (r==true)
{
removeArticle($(this).attr('name'));
}
});发布于 2014-03-28 05:03:37
https://stackoverflow.com/questions/22704559
复制相似问题