简体   繁体   English

JavaScript 在添加时崩溃<br>

[英]JavaScript is crashing at adding <br>

since many days, I'm stuck with if I want to add <br> with elem.innerHTML += '<br>' or document.createElement('br') , the whole site crashes.很多天以来,如果我想用elem.innerHTML += '<br>'document.createElement('br')添加<br> ,我就会坚持下去,整个网站都会崩溃。 But if I am doing this with a normal letter, it works just fine.但是,如果我使用普通字母来执行此操作,则效果很好。

 function setSpace(elem){ if((elem.clientHeight-49)/16 >= 1){ for(var i=0; i <= (elem.clientHeight-49)/16; i++){ elem.innerHTML += '<br>'; } } }

How can I solve this problem?我怎么解决这个问题?

The loop risks to never finish, because you extend the height of the element in every iteration, which is also influencing the stop-condition of the loop.循环可能永远不会完成,因为您在每次迭代中都扩展了元素的高度,这也影响了循环的停止条件。 elem.clientHeight increases, and i might never get greater than (elem.clientHeight-49)/16 . elem.clientHeight增加, i可能永远不会大于(elem.clientHeight-49)/16

A way out is to first get the limit into a variable, and then use that:一种出路是首先将限制放入变量中,然后使用它:

 function setSpace(elem){
    let end = (elem.clientHeight-49)/16;
    if (end >= 1){
      for(var i=0; i <= end; i++) {
        elem.innerHTML += '<br>';
      }
    }
  }

... but stil be careful, as the next time you call setSpace it will add even more line breaks. ...但仍然要小心,因为下次您调用setSpace时,它会添加更多换行符。 If you keep calling this function, you'll never stop adding line breaks.如果您继续调用此 function,您将永远不会停止添加换行符。

Review what your purpose really is with this code.查看您使用此代码的真正目的。

There are better ways to add spacing to an element, for instance with CSS padding-bottom .有更好的方法可以为元素添加间距,例如使用 CSS padding-bottom

I believe it's just same as this .我相信它和这个一样。

Every time innerHTML is set, the HTML has to be parsed, a DOM constructed, and inserted into the document.每次设置 innerHTML 时,都必须解析 HTML,构建 DOM 并将其插入到文档中。 This takes time.这需要时间。

That's causing your browser crashes.这会导致您的浏览器崩溃。 Actually not crashes, but it's still processing.实际上没有崩溃,但它仍在处理中。

The solution is, you need to use appendChild.解决方案是,您需要使用 appendChild。

function setSpace(elem) {
    var space = document.createElement('div');
    var count = (elem.clientHeight-49)/16;

    if (count >= 1) {
        for (var i=0; i <= count; i++) {
            elem.appendChild(space);
        }
    }
}
  1. Your code seems to work fine and doesnt crash.您的代码似乎工作正常并且不会崩溃。
  2. If you are trying to increase the height by adding extra如果您试图通过添加额外的来增加高度
    that would not be the right approach.这不是正确的做法。
    1. Rather suggest to change the height directly by changing the style properties而是建议通过更改样式属性直接更改高度

Try to use the appendChild() function instead:尝试使用appendChild() function 代替:

  function setSpace(elem){
    if((elem.clientHeight-49)/16 >= 1){
      for(var i=0; i <= (elem.clientHeight-49)/16; i++){
        elem.appendChild(br);
      }
    }
  }

You can try: elem.innerHTML += '\n'你可以试试: elem.innerHTML += '\n'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM