你好 超人 2022-04-19 19:00 采纳率: 20%
浏览 57

html 留言板的删除功能咋添加?


```html
</head>
<body>
    <div class="main">
        <h1>留言板:</h1>
        <div class='mess'>
            <textarea></textarea>
        </div>
        <br>
        <button>OK</button>
    </div>
</body>
<script>
$('treaexta').focus(function(){
    this.value='';
    
});//当鼠标点击留言框时,留言框内部内容清空[]()
$('button').click(function(){
    val=$('textarea').val();
    str="<div class='show'>";
    str+="<div class='close'>&times;</div>";
    str+=val;
    str+="</div>";
    $('.main').append(str);
});//点击button后下方出现留言框的内容
html

$('.close').live('click',function(){
    $(this).parent().hide(1000);
});//点击叉号后隐藏class=show的父标签
</script>






```html
  • 写回答

2条回答 默认 最新

  • 崽崽的谷雨 2022-04-19 19:37
    关注
    
    <!DOCTYPE html>
    <html lang="en">
    
    <head>
       <meta charset="UTF-8">
       <meta http-equiv="X-UA-Compatible" content="IE=edge">
       <meta name="viewport" content="width=device-width, initial-scale=1.0">
       <title>Document</title>
    </head>
    
    <body>
       <div class="main">
          <h1>留言板:</h1>
          <div class='mess'>
             <textarea></textarea>
          </div>
          <br>
          <button>OK</button>
       </div>
    </body>
    <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
    <script>
       $('treaexta').focus(function () {
          this.value = '';
    
       });//当鼠标点击留言框时,留言框内部内容清空[]()
       $('button').click(function () {
          val = $('textarea').val();
          str = "<div class='show'>";
          str += "<div class='close'  >&times;</div>";
          str += val;
          str += "</div>";
          $('.main').append(str);
          // 因为 一开始 没有 close元素 所以要放到 这个 追加万之后 添加点击事件
          if ($('.close').length > 0) { //为了防止 一开始 没有 close 这个dom 报错
    
    
             $(".close").click(function () {
                $(this).parent().hide(1000);
    
             });
          }
       });//点击button后下方出现留言框的内容
    
    
    
    
    
    
    
    </script>
    
    </html>
    

    可以参考一下 。

    live事件 一直报错 。
    还有就是 你直接 close 添加事件 是不对的,因为 你没有留言时 获取不到 关闭 按钮 。 你需要放到 appendchild之后 再添加事件

    评论

报告相同问题?

问题事件

  • 创建了问题 4月19日

悬赏问题

  • ¥100 对反编译和ai熟悉的开发者。
  • ¥15 带序列特征的多输出预测模型
  • ¥15 Python 如何安装 distutils模块
  • ¥15 关于#网络#的问题:网络是从楼上引一根网线下来,接了2台傻瓜交换机,也更换了ip还是不行
  • ¥15 资源泄露软件闪退怎么解决?
  • ¥15 CCF-CSP 2023 第三题 解压缩(50%)
  • ¥30 comfyui openpose报错
  • ¥20 Wpf Datarid单元格闪烁效果的实现
  • ¥15 图像分割、图像边缘提取
  • ¥15 sqlserver执行存储过程报错