最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Adding multiple(extra) white spaces to span using jQuery - Stack Overflow

programmeradmin0浏览0评论

So I have been looking for this question around and still didn't find an answer. I know how to add a single space with the append method in jQuery but don't know how to add multiple spaces.

Example:

<div id="#myDiv"> hey? <span class="space"></span> my name<span class="space"></span> is coding enthusiast<span class="space"></span> .....</di>

I want the output to be like this:

Hey          my name        is coding enthusiast        ......

I have tried Jquery append, it gives me nothing like that.

 //1st try 
 $('space').append("      ");
 //2nd try
 $('space').append("&nbsp &nbsp &nbsp &nbsp");
 //2nd try output: hey&nbsp&nbsp&nbspmy name&nbsp&nbsp&nbspis coding enthusiast&nbsp&nbsp&nbsp....

I feel like I am missing something small. Any ideas?

Edit: Had a typo in my code's question which misled some people when answering:

    //1st try 
     $('.space').append("      ");
     //2nd try
     $('.space').append("&nbsp &nbsp &nbsp &nbsp");
     //2nd try output: hey&nbsp&nbsp&nbspmy name&nbsp&nbsp&nbspis coding enthusiast&nbsp&nbsp&nbsp....

So I have been looking for this question around and still didn't find an answer. I know how to add a single space with the append method in jQuery but don't know how to add multiple spaces.

Example:

<div id="#myDiv"> hey? <span class="space"></span> my name<span class="space"></span> is coding enthusiast<span class="space"></span> .....</di>

I want the output to be like this:

Hey          my name        is coding enthusiast        ......

I have tried Jquery append, it gives me nothing like that.

 //1st try 
 $('space').append("      ");
 //2nd try
 $('space').append("&nbsp &nbsp &nbsp &nbsp");
 //2nd try output: hey&nbsp&nbsp&nbspmy name&nbsp&nbsp&nbspis coding enthusiast&nbsp&nbsp&nbsp....

I feel like I am missing something small. Any ideas?

Edit: Had a typo in my code's question which misled some people when answering:

    //1st try 
     $('.space').append("      ");
     //2nd try
     $('.space').append("&nbsp &nbsp &nbsp &nbsp");
     //2nd try output: hey&nbsp&nbsp&nbspmy name&nbsp&nbsp&nbspis coding enthusiast&nbsp&nbsp&nbsp....
Share Improve this question edited Apr 27, 2019 at 13:56 Shiladitya 12.2k17 gold badges28 silver badges42 bronze badges asked Jun 7, 2015 at 4:43 Coding EnthusiastCoding Enthusiast 3,9331 gold badge31 silver badges50 bronze badges 0
Add a ment  | 

4 Answers 4

Reset to default 3

fixed, forgot semicolon, Thanks to Jake's ment(which he deleted already), I could fix this issue.

  $('.space').append("&nbsp; &nbsp; &nbsp; &nbsp;");

You are not selecting your elements correctly and add semicolon. Try this:

$('.space').append("&nbsp;&nbsp;&nbsp;&nbsp;");

You need to include the . to specify you're selecting by a class, and add a semicolon after nbsp (as Jake Opena pointed out.)

$('.space').append("&nbsp; &nbsp; &nbsp; &nbsp;");

A different approach could be something like this: https://jsfiddle/fvLxxwa4/

Use this styling for .space:

.space {
    margin-left: 50px;
}
发布评论

评论列表(0)

  1. 暂无评论