2014-02-17 68 views
-1

我有字符串:如何用HTML標籤替換字符串中的文本?

" Text01 [1] text02 [2] text03 [3] " 

,我需要更換[1] [2] [3]input type="text"

我需要它來創建一個的JavaScript填充式的毛坯測驗。

我做了這樣的事情:

$("span").click(function() 
{ 
    var input = $("<input>", { val: $(this).text(), type: "text" }); 

    $(this).replaceWith(input); 

    input.select(); 
}); 

我必須[1] [2] [3]和之間添加一些標記的是,我不知道如何做的jQuery的一部分。

+2

那麼,你有什麼嘗試? – putvande

回答

0

嗯...也許這樣的事情?

var str = " Text01 [1] text02 [2] text03 [3] "; 

str = str.replace(/\[[0-9]+\]/g, function(match) { 
    return '<span id="'+match+'"><input type="text"></span>'; 
}); 

alert(str); //outputs Text01 <span id="[1]"><input type="text" /></span>... 
+0

謝謝......這是他的工作...... –

+0

請標記我接受的答案。 – aksu

+0

這是他的工作,但不是我真正想要的......我想用一個唯一的ID來覆蓋[1]之間的範圍......這樣我就可以檢索到我替換的值... –

相關問題