如何在jquery中追加4-5个空格?

时间:2017-05-30 08:58:38

标签: javascript jquery

  

我试图通过以下代码附加4-5个apces,但事实并非如此   工作。 Jquery的新手,JS请帮助。

$("#<%= lbl100.ClientID %>").text("Your search request is too broad and the first 100 results are displayed.Please refine your search if your result does not show.").append("            ");

注意: - 我最后也使用了&nbspadding space(没有工作),但它们按原样打印,空间不到了

2 个答案:

答案 0 :(得分:1)

&nbsp;html()一起使用,并确保ID: - <%= lbl100.ClientID %>正确且存在。

实施例: -

&#13;
&#13;
$("#abc").html("Your search request is too broad and the first 100 results are displayed.Please refine your search if your result does not show.      ").append("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="abc"></div>
&#13;
&#13;
&#13;

注意: - 如果您尝试选择文本,则会看到也选择了空格。

另一种方法: -

&#13;
&#13;
$("#abc").html("Your search request is too broad and the first 100 results are displayed.Please refine your search if your result does not show.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;see the space");
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="abc"></div>
&#13;
&#13;
&#13;

注意: - 确保在脚本代码之前添加了jQuery库,否则它将无效并且您的浏览器控制台中会出现$ is undefined错误。

答案 1 :(得分:1)

HTML的工作原理:white-space is collapsed到一个空格中。如果您需要硬编码的重复空格,则需要插入文字U+00A0 NO-BREAK SPACE字符。有几种方法可以做到:

  • 按原样输入(您也可以使用剪贴板)
  • 插入JavaScript实体
  • 插入HTML实体

Snippet说明了#2和#3(编辑器会将角色转换为常规空间):

&#13;
&#13;
$("div:nth-of-type(1)").text("One\u00A0\u00A0\u00A0\u00A0Two");
$("div:nth-of-type(2)").html("One&nbsp;&nbsp;&nbsp;&nbsp;Two");
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div></div>
<div></div>
&#13;
&#13;
&#13;