开发者社区> 问答> 正文

关于点击按钮后怎么才能不让前面的remove消失的问题

代码如下:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style type="text/css">
.fly{
    float:left;
    background:rgb(56,62,70);
    height:160px;

}
</style>
<script src="jquery.js"></script>
</head>
<body>
<div><h1>Vacation Packages</h1><p></div>
<div class="fly">
<h2>1.Hawaiian Vacation</h2>
<ul>
    <li class="vacation">comments on this deal:<br />
    "Amaring Deal!!"<br />
    "Can't wait to take this trip!"<br />
    <button type=button>GET PRICE</button></li>
</ul>
</div>
<div class="fly">
<h2>2.Hawaiian Vacation</h2>
<ul>
    <li class="vacation">comments on this deal:<br />
    "Amaring Deal!!"<br />
    "Can't wait to take this trip!"<br />
    <button type=button>GET PRICE</button></li>
</ul>
</div>
<div class="fly">
<h2>3.Hawaiian Vacation</h2>
<ul>
    <li class="vacation">comments on this deal:<br />
    "Amaring Deal!!"<br />
    "Can't wait to take this trip!"<br />
    <button type=button>GET PRICE</button></li>
</ul>
</div>
</body>
<script type="text/javascript">
$('document').ready(function(){
        var price=$('<span style="font-size:14px; color:red"><strong>from 5000</strong></span>');
        $('button').on('click',function(){
            //$('.vacation').append(price);
//          $(this).closest('.vacation').append(price);
            $(this).after(price);
            $(this).remove();

        });
});
</script>
</html>

求解答,为什么会消失,怎么才能不消失?

展开
收起
吴孟桥 2016-05-31 02:40:01 2015 0
1 条回答
写回答
取消 提交回答
  • var price=$('<span style="font-size:14px; color:red"><strong>from 5000</strong></span>');
    
                    ======>
    
    
            var price = '<span style="font-size:14px; color:red"><strong>from 5000</strong></span>';

    你用$生成后就是对象了,DOM操作会将对象移动到当前插入的位置。变为字符串就不是dom对象了,会生成新的dom对象

    2019-07-17 19:21:09
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载