2009-09-30 152 views
17

我试图使用jQuery追加空格。这些示例都不工作:使用jQuery追加空格

 
    $("#mySelector").append($(" ")); 
    $("#mySelector").append($(" ")); 

任何想法?

回答

47

如何

$("#mySelector").append(" "); // or with & nbsp; 
+3

这是jQuery的正确的答案,或者你可以只使用CSS:#mySelector {填充右:1EM; } – Mottie 2009-10-01 03:36:50

+0

谢谢你,Ólafur。我太努力了:) – 2009-10-01 15:09:05

-3

未经测试(可能有点矫枉过正):

$("").append($("<p> </p>").text()); 
-1

;创建一个jQuery插件功能重用它时,你需要把空间。这样你将始终保持一致。

if(!$.space) { 
     $.space = function​(noOfSpaces) { 
      var space = " ", returnValue = ""; 
      for(var index=0; index < noOfSpaces; index++) { 
       returnValue += space; 
      } 
      return returnValue; 
     } 
    } 

alert("Stack" + $.space(6) + "Overflow"); 
+0

这与OP的问题无关,他没有问如何创建一个空间填充字符串。 – 2017-03-03 16:55:37

5

在我来说,我做了以下内容:

$('.colwid10a').each(function() { 
    if ($(this).is(':empty')) { 
     $(this).append("&nbsp;"); 
    } 
}); 
$('.colwid12').each(function() { 
    if ($(this).find('a').is(':empty')) { 
     $(this).find('a').append("&nbsp;"); 
    } 
});