24

我正在尝试使用 jQuery 附加一个空格。这些样本都不起作用:

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

有任何想法吗?

4

5 回答 5

57

怎么样

$("#mySelector").append(" "); // or with & nbsp;
于 2009-09-30T22:56:44.527 回答
5

就我而言,我做了以下事情:

$('.colwid10a').each(function () {
    if ($(this).is(':empty')) {
        $(this).append(" ");
    }
});
$('.colwid12').each(function () {
    if ($(this).find('a').is(':empty')) {
        $(this).find('a').append(" ");
    }
});
于 2013-03-21T11:19:14.957 回答
0

你也可以使用'\xa0',它是一个非换行符。

$("#mySelector").append('\xa0');
于 2021-10-01T15:52:16.393 回答
-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");
于 2012-09-05T11:47:56.427 回答
-4

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

$("").append($("<p> </p>").text());
于 2009-09-30T23:05:28.953 回答