2013-03-14 21 views
0

我都這樣了,它的工作原理:文本附加到一個div,而不是HTML

local.Source = $('article.hero-unit').html(); 
$('#Source').text(local.Source); 

但我想要做的是這樣的:

local.Source = $('article.hero-unit').html(); 
$('fieldset').append(local.Source); 

的問題是, append方法附加html並且不會轉義<符號。

回答

1

嘗試

local.Source = $('article.hero-unit').html(); 
var $fieldset = $('fieldset'); 
$fieldset.text($fieldset.text()+local.Source); 

調用.text()不帶參數已經抓住文本的元素。所有這一切確實是local.Source的HTML代碼添加到它

+0

好吧,我的問題是我失去了我的。 – 2013-03-15 00:12:40

+0

@Phillip我不知道我理解你的問題,我假設你的圖例標記嵌套在你的字段集中,這意味着我的代碼應該工作。你介意創建一個小提琴(codepen.io或jsfiddle.net或jsbin.com)來說明這個代碼的問題,或者至少說出預期的輸入和輸出是什麼? – 2013-03-15 00:15:36

+0

@Phillip你最終做了什麼?有什麼問題? – 2013-03-21 21:15:23

1

我會把文本變成<span>元素:

$('<span>', { 
    text: local.Source 
}).appendTo('fieldset'); 
+0

嗨,我想知道爲什麼你認爲這是比我提出的更好的解決方案? (順便說一句,你是個野獸:)偉大的工作!) – 2013-03-14 23:52:50

+0

@BenjaminGruenbaum:這只是另一種方式。包裝元素讓您可以對文本塊進行樣式設計,並使之更容易將其刪除。 – Blender 2013-03-14 23:55:48

0

我覺得這個做的伎倆:

local.Source = $('article.hero-unit').html(); 
local.Legend = $('fieldset legend').text(); 
$('fieldset').text(local.Source); 
$('fieldset').prepend('<legend>' + local.Legend + '</legend>');