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

javascript - Adding space in a string jquery - Stack Overflow

programmeradmin4浏览0评论

I wanto to insert a space at the start of my string Example:

MySite
   MySite (I want this)

I have tried in this mode but nothing

txt="    "+v.text();

Can someone help me? Thanks a lot

I wanto to insert a space at the start of my string Example:

MySite
   MySite (I want this)

I have tried in this mode but nothing

txt="    "+v.text();

Can someone help me? Thanks a lot

Share Improve this question edited Jul 3, 2012 at 10:10 Zoltan Toth 47.7k12 gold badges131 silver badges137 bronze badges asked Jul 3, 2012 at 10:09 user1427811user1427811 8
  • 3 I'm unclear what you want to achieve. Show us the before and after string. If you mean you want to prepend a string with a space, the code you posted is correct (though I don't know why you included five spaces rather than just one). – Mitya Commented Jul 3, 2012 at 10:11
  • remember that html browsers fold multiple adjacent whitespaces into one – wroniasty Commented Jul 3, 2012 at 10:11
  • can you do that in css instead? – Bob Commented Jul 3, 2012 at 10:12
  • Use      MySite – coolguy Commented Jul 3, 2012 at 10:12
  • and what is the context for this? you only want your txt var to have this spaces or is it inside some kind of html structure? – Th0rndike Commented Jul 3, 2012 at 10:12
 |  Show 3 more ments

4 Answers 4

Reset to default 7

Try :

 txt="    "+v.text();

The other good solution would be to use div with some style set on it.

 <div style='margin-left:15px' id='main_text'></div>

You could use jQuery to place txt in the div.

     txt= v.text();
     $('#main_text').html(txt);

A solution is to fill the spaces with &nbsp; entities, but if you have to achieve this effect just for styling/visualization purpose, then wrap that line in a proper tag and use some CSS instead (e.g. text-indent property)

if you have to indent text inside a select (as I guess reading your previous answer) you may want to wrap your options inside an optgroup element (and give it some padding)

var str2 = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Here goes your string with spaces";

There are 2 solutions below :-

1. txt="<pre>    </pre>"+v.text();


2. txt="<span style='margin-left:15px'></span>"+v.text();
发布评论

评论列表(0)

  1. 暂无评论