2013-03-28 62 views
1

有了這個網站:字符串替換使用jQuery

<blockquote class="stat"> 
    There are &rsquo;paper cups&lsquo; in there. 
</blockquote> 

我使用這個位的jQuery:

function replaceEntity(element, entity, replacement) { 
    $(element).each(function() { 
    $(this).html(function(i, html) { 
     return html.replace('‘', '<span class="rsquo">&rsquo;</span>').replace('’', '<span class="lsquo">&lsquo;</span>'); 
    }); 
    }); 
} 

生產這樣的:

<blockquote class="stat"> 
    There are <span class="rsquo"><span class="lsquo">‘</span></span>paper cups<span class="lsquo"><span class="rsquo">’</span></span> in there. 
</blockquote> 

,但我想這一點:

<blockquote class="stat"> 
    There are <span class="rsquo">&rsquo;</span>paper cups<span class="lsquo">&lsquo;</span> in there. 
</blockquote> 

我如何才能

  1. 獲取搜索掃描兩個實體沒有嵌套?
  2. 停止jQuery替換與UTF-8等效的html實體?

編輯

嵌套錯誤被鏈接到另一個開發者文檔的另一個片段。我會最終刪除這個問題。

+0

一旦您插入實體'HTML()'它們轉換爲實際的字符,因爲這是被認爲碰巧實際顯示角色而不是實體。你可以使用'text()',但是也會暴露'span'標籤 - > [** FIDDLE **](http://jsfiddle.net/etkZy/)? – adeneo 2013-03-28 00:17:22

+0

jQuery在這裏沒有什麼可做的,因爲你所要求的字符串替換是普通的JavaScript。 – sp00m 2013-03-28 00:27:00

+0

是不是html實體是不是非常重要的,嵌套跨度的問題是由於在另一個開發工作循環。去搞清楚。 – Niels 2013-03-28 00:40:47

回答

0

您可以使用此

$('.stat').text('There are <span class="rsquo">&rsquo;</span>paper cups<span class="lsquo">&lsquo;</span> in there.').html(); 

,但它沒有解釋像一個HTML元素