壶壶壶壶壶壶壶 2023-05-16 03:21 采纳率: 100%
浏览 28
已结题

关于#javascript#的问题:想请问下 我想实现else时的close()会报错the node to be removed is not a child of this node是什么原因

各位好,想请问下 我想实现else时的close()会报错the node to be removed is not a child of this node是什么原因?

img

如果我想实现再次点击删除按钮时关闭弹窗怎么做?以下是源代码。

img

<!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>面向对象封装消息提示</title>
  <style>
    .modal {
      width: 300px;
      min-height: 100px;
      box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
      border-radius: 4px;
      position: fixed;
      z-index: 999;
      left: 50%;
      top: 50%;
      transform: translate3d(-50%, -50%, 0);
      background-color: #fff;
    }

    .modal .header {
      line-height: 40px;
      padding: 0 10px;
      position: relative;
      font-size: 20px;
    }

    .modal .header i {
      font-style: normal;
      color: #999;
      position: absolute;
      right: 15px;
      top: -2px;
      cursor: pointer;
    }

    .modal .body {
      text-align: center;
      padding: 10px;
    }

    .modal .footer {
      display: flex;
      justify-content: flex-end;
      padding: 10px;
    }

    .modal .footer a {
      padding: 3px 8px;
      background: #ccc;
      text-decoration: none;
      color: #fff;
      border-radius: 2px;
      margin-right: 10px;
      font-size: 14px;
    }

    .modal .footer a.submit {
      background-color: #369;
    }
  </style>
</head>

<body>
  <button id="delete">删除</button>
  <button id="login">登录</button>

  <!-- <div class="modal">
    <div class="header">温馨提示 <i>x</i></div>
    <div class="body">您没有删除权限操作</div>
  </div> -->


  <script>
    // 1.  模态框的构造函数
    function Modal(title = '', message = '') {
      // 公共的属性部分
      this.title = title
      this.message = message
      // 因为modal盒子是公共的,一定不要忘了加 this 去形成不同的对象
      // 1. 创建 
      // 自定modalBox方法:创建div标签
      this.modalBox = document.createElement('div')
      // 2. 添加类名
      this.modalBox.className = 'modal'
      // 3. 填充内容 更换数据
      this.modalBox.innerHTML = `
        <div class="header">${this.title} <i>x</i></div>
        <div class="body">${this.message}</div>
      `
      // console.log(this.modalBox)
    }
    // 2. 打开方法 写到 模态框的构造函数原型身上
    Modal.prototype.open = function () {//用this时不能用箭头函数
      if (!document.querySelector('.modal')) {
        // 把刚才创建的盒子 modalBox  渲染到 页面中  父元素.appendChild(子元素)
        // document.body.appendChild(this.modalBox) //别忘记加this
        document.body.append(this.modalBox)
        //原型对象里的this指向实例对象
        // 获取 x  调用关闭方法
        this.modalBox.querySelector('i').addEventListener('click', () => {
          // 箭头函数没有this 上一级作用域的this
          // 这个this 指向 m 实例对象,如果用普通函数会指向i
          this.close()
        })
      }
      else{
        console.log(this.modalBox);
        this.close()
        // document.body.removeChild(this.modalBox)
    
      }
    }
    // 3. 关闭方法 挂载 到 模态框的构造函数原型身上
    Modal.prototype.close = function () {
      // document.body.removeChild(this.modalBox)
      this.modalBox.remove()
    }

    // 4. 按钮点击
    document.querySelector('#delete').addEventListener('click', () => {
      const m = new Modal('温馨提示', '您没有权限删除')
      // 调用 打开方法
      m.open()
    })

    // 5. 按钮点击
    document.querySelector('#login').addEventListener('click', () => {
      const m = new Modal('友情提示', '您还么有注册账号')
      // 调用 打开方法
      m.open()
    })

  </script>
</body>

</html>

  • 写回答

3条回答 默认 最新

  • 崽崽的谷雨 2023-05-16 09:10
    关注

    要删除的节点不是此节点的子节点 .

    else 里 写成 document.body.removeChild(document.querySelector('.modal')); 或者 document.querySelector('.modal').remove();

    removeChild 删除的子元素 必须是 用夫级调用。 因为 this.modalBox 他不是一个 dom元素,你需要 先获取再删除。

    其实 你删除元素 应该获取元素再删除,不能拿 你定义的变量来,完整例子是下面的代码

    <!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>面向对象封装消息提示</title>
      <style>
        .modal {
          width: 300px;
          min-height: 100px;
          box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
          border-radius: 4px;
          position: fixed;
          z-index: 999;
          left: 50%;
          top: 50%;
          transform: translate3d(-50%, -50%, 0);
          background-color: #fff;
        }
     
        .modal .header {
          line-height: 40px;
          padding: 0 10px;
          position: relative;
          font-size: 20px;
        }
     
        .modal .header i {
          font-style: normal;
          color: #999;
          position: absolute;
          right: 15px;
          top: -2px;
          cursor: pointer;
        }
     
        .modal .body {
          text-align: center;
          padding: 10px;
        }
     
        .modal .footer {
          display: flex;
          justify-content: flex-end;
          padding: 10px;
        }
     
        .modal .footer a {
          padding: 3px 8px;
          background: #ccc;
          text-decoration: none;
          color: #fff;
          border-radius: 2px;
          margin-right: 10px;
          font-size: 14px;
        }
     
        .modal .footer a.submit {
          background-color: #369;
        }
      </style>
    </head>
     
    <body>
      <button id="delete">删除</button>
      <button id="login">登录</button>
     
      <!-- <div class="modal">
        <div class="header">温馨提示 <i>x</i></div>
        <div class="body">您没有删除权限操作</div>
      </div> -->
     
     
      <script>
        // 1.  模态框的构造函数
        function Modal(title = '', message = '') {
          // 公共的属性部分
          this.title = title
          this.message = message
          // 因为modal盒子是公共的,一定不要忘了加 this 去形成不同的对象
          // 1. 创建 
          // 自定modalBox方法:创建div标签
          this.modalBox = document.createElement('div')
          // 2. 添加类名
          this.modalBox.className = 'modal'
          // 3. 填充内容 更换数据
          this.modalBox.innerHTML = `
            <div class="header">${this.title} <i>x</i></div>
            <div class="body">${this.message}</div>
          `
          // console.log(this.modalBox)
        }
        // 2. 打开方法 写到 模态框的构造函数原型身上
        Modal.prototype.open = function () {//用this时不能用箭头函数
          if (!document.querySelector('.modal')) {
            // 把刚才创建的盒子 modalBox  渲染到 页面中  父元素.appendChild(子元素)
            // document.body.appendChild(this.modalBox) //别忘记加this
            document.body.append(this.modalBox)
            //原型对象里的this指向实例对象
            // 获取 x  调用关闭方法
            this.modalBox.querySelector('i').addEventListener('click', () => {
              // 箭头函数没有this 上一级作用域的this
              // 这个this 指向 m 实例对象,如果用普通函数会指向i
              this.close()
            })
          }
          else{
            //document.body.removeChild(document.querySelector('.modal'));
            //document.querySelector('.modal').remove();
            this.close()
          }
        }
        // 3. 关闭方法 挂载 到 模态框的构造函数原型身上
        Modal.prototype.close = function () {
          // document.body.removeChild(this.modalBox)
          //this.modalBox.remove()
          document.querySelector('.modal').remove();
        }
     
        // 4. 按钮点击
        document.querySelector('#delete').addEventListener('click', () => {
          const m = new Modal('温馨提示', '您没有权限删除')
          // 调用 打开方法
          m.open()
        })
     
        // 5. 按钮点击
        document.querySelector('#login').addEventListener('click', () => {
          const m = new Modal('友情提示', '您还么有注册账号')
          // 调用 打开方法
          m.open()
        })
     
      </script>
    </body>
     
    </html>
     
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 5月24日
  • 已采纳回答 5月16日
  • 修改了问题 5月16日
  • 修改了问题 5月16日
  • 展开全部

悬赏问题

  • ¥15 mmo能不能做客户端怪物
  • ¥15 osm下载到arcgis出错
  • ¥15 Dell g15 每次打开eiq portal后3分钟内自动退出
  • ¥200 使用python编写程序,采用socket方式获取网页实时刷新的数据,能定时print()出来就行。
  • ¥15 matlab如何根据图片中的公式绘制e和v的曲线图
  • ¥15 我想用Python(Django)+Vue搭建一个用户登录界面,但是在运行npm run serve时报错了如何解决?
  • ¥15 QQ邮箱过期怎么恢复?
  • ¥15 登录他人的vue项目显示服务器错误
  • ¥15 (标签-android|关键词-app)
  • ¥15 comsol仿真压阻传感器