2017-03-03 35 views
1

我有一個span元素在我的Dom這樣插入空格內跨度爲文本失敗

<span class="intro">Create your account inng to some worthy causes.</span> 

我想跨度 我的代碼是這樣的

裏面的最後一個字符後追加一個空格
$($('.sp-pods h2 span')).each(function() { 
    $(this).text($(this).text() + '&nbsp;'); 
}); 

一切看起來都對我好,但空白空間沒有得到添加。但如果我嘗試這樣的東西

$($('.sp-pods h2 span')).each(function() { 
    $(this).text($(this).text() + 'xx';'); 
}); 

它運作良好/只有空白不來。我在這裏做錯了什麼?

+0

'$($( 'SP-莢H2跨度 '))'應該是'$(' SP-莢H2跨度')' –

+0

@madalinivascu你能給我一個更好的辦法。我都接受所有建議 – Athul

+0

嘗試使用CSS規則。 –

回答

1

你可以使用padding-rightmargin-right,如:

span.intro{ 
    padding-right: 5px; 
} 

或者:

span.intro{ 
    margin-right: 5px; 
} 

希望這有助於。

span.intro{ 
 
    margin-right: 20px; 
 
}
<span class="intro">Create your account inng to some worthy causes.</span>TEXT AFTER SPACE

0
&nbsp; is not accepted. 

,你可以簡單地添加到空間功能您呼叫

$($('.sp-pods h2 span')).each(function() { 

    $(this).text($(this).text() + ' '); 
}); 

,或者你可以用 '\ u00a0'

$($('.sp-pods h2 span')).each(function() { 

$(this).text($(this).text() + '\u00a0'); 

})代替 ;

1

您應該使用這個CSS僞元素。

.intro::after { 
    content: ' '; 
}