我正在尝试使用 jQuery 附加一个空格。这些样本都不起作用:
$("#mySelector").append($(" ")); $("#mySelector").append($(" "));
有任何想法吗?
怎么样
$("#mySelector").append(" "); // or with & nbsp;
就我而言,我做了以下事情:
$('.colwid10a').each(function () {
if ($(this).is(':empty')) {
$(this).append(" ");
}
});
$('.colwid12').each(function () {
if ($(this).find('a').is(':empty')) {
$(this).find('a').append(" ");
}
});
你也可以使用'\xa0',它是一个非换行符。
$("#mySelector").append('\xa0');
并且,创建一个 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");
未经测试(可能有点矫枉过正):
$("").append($("<p> </p>").text());