2009-09-30 148 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;"); 
    } 
});