首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何隐藏动态创建的div

如何隐藏动态创建的div
EN

Stack Overflow用户
提问于 2014-03-28 04:59:29
回答 5查看 1.2K关注 0票数 0

如何向动态创建的div添加诸如隐藏之类的事件?

php生成下面的html代码,然后我将其附加到页面中。之后,我可以点击delete li删除整个文章div或隐藏它。

我试过了,但没有起作用。

代码语言:javascript
复制
$( '.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'));
        }
});
EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2014-03-28 05:03:53

尝尝这个,

代码语言:javascript
复制
$(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是错误的,试试这个,

代码语言:javascript
复制
<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>
票数 0
EN

Stack Overflow用户

发布于 2014-03-28 05:02:14

使用在……上面 JQuery函数

代码语言:javascript
复制
$(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'));
        }
 });
票数 0
EN

Stack Overflow用户

发布于 2014-03-28 05:03:37

使用jquery 在……上面,如

代码语言:javascript
复制
$(document).on("click","li",function()
{
//your code
});

请参阅下面的链接

https://learn.jquery.com/events/event-delegation/

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22704559

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档