动态创建一个空白的 Html 空间 Javascript

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10951340/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 03:44:21  来源:igfitidea点击:

Create A Blank Html Space On The Fly Javascript

javascript

提问by Nick LaMarca

How do I create a blank space on the fly using javascript? The following is not working for me..

如何使用 javascript 动态创建一个空白空间?以下对我不起作用..

var blank = document.createElement(" ");
div.appendChild(blank);

采纳答案by Sirko

If you really just want to append a space character, use the following:

如果您真的只想附加一个空格字符,请使用以下命令:

div.innerHTML += ' ';

or

或者

div.innerHTML += ' ';

for the HTML entity.

对于 HTML 实体。

回答by Moin Zaman

Try using this:

尝试使用这个:

myElement.appendChild( document.createTextNode( '\u00A0' ) );

If you want/need more spaces, use several \u00A0e.g.:

如果您想要/需要更多空间,请使用多个\u00A0例如:

myElement.appendChild( document.createTextNode( '\u00A0\u00A0' ) );


This one:

这个:

document.createEntityReference('nbsp');

doesn't work all browsers, so avoid it.

不适用于所有浏览器,因此请避免使用它

回答by Alex Gamezo

You're looking for document.createTextNode(' ')I believe.

你在找document.createTextNode(' ')我相信。

This can only be used for plain text though, not for entities.

但这只能用于纯文本,不能用于实体。

回答by Daniel Azamar

You can use this:

你可以使用这个:

document.getElementById("your-button").firstChild.data = '\u00A0';

it worked for me ;)

它对我有用;)

回答by Jeff Watkins

document.getElementById("mydiv").innerHTML +="!";

works (I use exclaimation for immediacy of results, replace with what you wish.

有效(我使用感叹号来获得结果的即时性,用你想要的替换。

回答by Tom

you could do that as follows; but if you want some empty space maybe css would work better (margin, padding, ...)

你可以这样做;但如果你想要一些空白空间,也许 css 会更好(边距,填充,...)

var span = document.createElement("span");
span.innerHTML = " "; // (if this doesn't work try " ")
anyElement.appendChild(span); // make sure anyElement actually points to a DOM element

回答by GaneshB

This could help!!

这可能有帮助!!

var blank = document.createElement("br");
    /*this creates a br(for a break) element dynamically */
myElement.appendChild(blank);
    /* myElement could be any HTML element like div , 
    span , img etc created dynamically , 
    var myElement = document.createElement("your element") */        

You could append br as many times you want to get that many blank lines.

您可以多次附加 br 以获得那么多空行。

回答by Narendra Sagadevan

<body>
<table id="nadis">

</tr>
</table>

</body>

var numarray = ["1.1", "12.20", "151.12", 1000.23,12451.0733];
    var highetlen = 0;
    for(var i=0; i<numarray.length; i++){
        var n = numarray[i].toString();
      var res= n.split(".");
      n = res[0];

      if(highetlen < n.length){
        highetlen = n.length;
      }

    }

    for(var j=0; j<numarray.length; j++){
        var s = numarray[j].toString();
      var res= s.split(".");
      s = res[0];

        if(highetlen > s.length){
      var finallevel = highetlen - s.length;

      var finalhigh = "";
      for(k=0;k<finallevel;k++){
      finalhigh = finalhigh+ '&#160; ';
      }
        numarray[j] = finalhigh + numarray[j];
      }
      var nadiss = document.getElementById("nadis");
      nadiss.innerHTML += "<tr><td>" + numarray[j] + "</td></tr>";
    }