使用 jQuery 附加一个空格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1501087/
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-26 11:36:23  来源:igfitidea点击:

Append a space with jQuery

jqueryappend

提问by Joel Harris

I'm trying to append a space using jQuery. Neither of these samples work:

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

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

Any ideas?

有任何想法吗?

回答by ólafur Waage

How about

怎么样

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

回答by Jatinder

In my case I did the following:

就我而言,我执行了以下操作:

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

回答by Ramiz Uddin

And, create a JQuery Plugin function to reuse it whenever you need to put space. This way you will be consistent throughout.

并且,创建一个 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");

回答by cllpse

Untested (and probably a bit overkill):

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

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