dilongzuyj 2024-02-03 10:56 采纳率: 85.7%
浏览 12

这是一个不知为啥的前端问题

问题很奇怪,我本来想编一个辅助创建ui的js库,but,出错了

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<style>
#area{
  background-color: #AEEAAE;
  width: 600px;
  height: 450px;
}
#ball{
  background-color: #BAF;
  width: 30px;
  height: 30px;
  border-radius: 30px;
  position: absolute;
  top: 0px;
  left: 0px;
}
</style>
</head>
<body>
<script>
var ui = {
  writeDOM:function(td){
    document.write();
    document.write(td);
  },
  setTitle:function(txt){
    document.getElementsByTagName("title")[0].innerHTML = txt;
  },
  newTitle:function(){
    let title = document.createElement("title");
    document.head.appendChild(title);
  },
  addTo:function(ele, to){
    to.appendChild(ele);
  },
  labelAdd:function(label, input, p){
    let ltg  = document.createElement("label");
    let txt = document.createTextNode(label);
    ltg.appendChild(txt);
    ltg.appendChild(input);
    p.appendChild(ltg);
  },
  setSize:function setSize(who, size, wb, hb){
    who.style.width =(size / 2 * wb)+ "px";
    who.style.height =(size / 2 * hb)+ "px";
  },
  message:function(text, size, l, h, p){
    let b1 = document.createElement("div"), b2 = document.createElement("div");
    b1.setAttribute("class", "button1");
    b2.setAttribute("class", "button2");
    let agent = document.createElement("div");
    agent.setAttribute("class", "agent");
    agent.setAttribute("style", `position:relative;top:${h}px;`);
    let txt = document.createElement("span");
    txt.setAttribute("class", "txt");
    txt.setAttribute("style", "position:absolute;top:30px;");
    agent.appendChild(b1);
    agent.appendChild(b2);
    agent.appendChild(txt);
    ui.setSize(agent, size, 2, 1);
    agent.style.backgroundColor = "grey";
    agent.style.left = l;
    txt.innerHTML = text;
    var x = l +(size / 2);
    b1.setAttribute("style", "position:absolute;");
    b2.setAttribute("style", "position:absolute;");
    b1.style.left = '-1px';
    b2.style.left = `${size / 2 + 1}px`;
    b1.style.top = `${size / 2}px`;
    b2.style.top = `${size / 2}px`;
    var os = size / 2;
    ui.setSize(b1, os, 2, 1);
    ui.setSize(b2, os, 2, 1);
    b1.style.backgroundColor = "#555";
    b2.style.backgroundColor = "#555";
    b1.setAttribute("onclick", `${b1}.remove;${b2}.remove;${txt}.remove;${agent}.remove;`);
    b2.setAttribute("onclick", `${b1}.remove;${b2}.remove;${txt}.remove;${agent}.remove;`);
    p.appendChild(agent);
  }
}
//实例:

//这段代码用于添加并修改标题
ui.newTitle();
ui.setTitle("小球撞撞撞");

var ball = document.createElement("div");
var area = document.createElement("div");
ball.setAttribute("id", "ball");
area.setAttribute("id", "area");

//这段代码添加元素
ui.addTo(area, document.body);
ui.addTo(ball, area);

var inp = document.createElement("input");

//这段代码用来给input标签添加label
ui.labelAdd("控制方向>>>", inp, document.body);

//这段代码可以创造一个框
ui.message("欢迎你!", 250, 250, 0, document.body);
</script>
</body>
</html>

求答!

  • 写回答

1条回答 默认 最新

  • 专家-赤兔[在线] 优质创作者: 编程框架技术领域 2024-02-03 10:59
    关注

    引自免费微信小程序:皆我百晓生

    根据您提供的代码,前端问题出在以下几点:

    1. document.write() 的使用不正确writeDOM 函数中,document.write() 方法被调用但没有传入任何参数。而且,在现代网页开发中,直接使用 document.write() 通常不是一个好的实践,因为它会重写整个文档流,尤其是在文档已经加载完毕后再调用时。

      // 建议改为创建或修改 DOM 元素的方式
      var ui = {
        writeDOM: function(td) {
          let container = document.createElement('div');
          container.innerHTML = td;
          document.body.appendChild(container);
        },
      // ...
      
    2. 事件绑定的错误message 函数中,给按钮元素添加了 onclick 属性来绑定点击事件,但是这种方式是旧版 JavaScript 的做法,容易出错,并且在这里它尝试直接删除元素的方法也是错误的。应使用 addEventListener 方法来正确绑定事件处理函数。

      // 修改为使用 addEventListener
      ui.message = function(text, size, l, h, p) {
        // ...
        b1.addEventListener("click", function() {
          agent.remove();
          txt.remove();
          b1.remove();
          b2.remove();
        });
        b2.addEventListener("click", function() {
          agent.remove();
          txt.remove();
          b1.remove();
          b2.remove();
        });
        // ...
      }
      
    3. 潜在的问题 虽然代码可以运行,但还有一些改进点:

      • 您可能希望将生成的消息框、文本、按钮等附加到特定父元素上,而不是直接附加到 document.body
      • 不要在 HTML 中直接编写脚本,而是将其放在 <script> 标签中或者外部文件中,然后在合适的位置(如 DOMContentLoaded 事件触发后)执行初始化和创建 UI 的逻辑。

    针对以上问题,优化后的 message 函数以及相关部分的代码如下:

    var ui = {
      // ... 其他函数不变 ...
    
      message: function(text, size, l, h, p) {
        let b1 = document.createElement("div"), 
            b2 = document.createElement("div"),
            agent = document.createElement("div"),
            txt = document.createElement("span");
    
        // 设置样式及属性
        // ... 与原代码相同的部分 ...
    
        // 绑定点击事件
        b1.addEventListener("click", function() {
          agent.remove();
          txt.remove();
          b1.remove();
          b2.remove();
        });
        b2.addEventListener("click", function() {
          agent.remove();
          txt.remove();
          b1.remove();
          b2.remove();
        });
    
        // 添加到目标父元素 p
        p.appendChild(agent);
    
        // 将文本添加到 span 中
        txt.innerHTML = text;
    
        // 添加其他子元素到 agent
        agent.appendChild(txt);
        agent.appendChild(b1);
        agent.appendChild(b2);
      }
    };
    
    // 确保在 DOMContentLoaded 后执行实例化操作
    document.addEventListener('DOMContentLoaded', function() {
      // ... 初始化标题、添加元素等操作 ...
    
      // 创建消息框并附加到 area 上
      ui.message("欢迎你!", 250, 250, 0, area);
    });
    

    这样修正之后,您的库应该能够正常工作,同时遵循了更好的前端编程实践。

    评论

报告相同问题?

问题事件

  • 创建了问题 2月3日