我想操縱DOM一點,需要一些幫助。將括號替換爲span javascript
這是我的HTML標記:
<span class=「content「> This is my content: {#eeeeee}grey text{/#eeeeee} {#f00000}red text{/#f00000}</span>
這是應該的:
<span class="content">This is my content: <span style="color:#eeeeee;">grey text</span><span style="color:#f00000;">red text</span></span>
的腳本應該以span標籤更換支架來改變字體顏色。 顏色應該與括號中的顏色相同。
我的方法:
function regcolor(element) {
var text = element.innerText;
var matches = text.match(/\{(#[0-9A-Fa-f]{6})\}([\s\S]*)\{\/\1\}/gim);
if (matches != null) {
var arr = $(matches).map(function (i, val) {
var input = [];
var color = val.slice(1, 8);
var textf = val.slice(9, val.length - 10);
var html = "<span style=\"color: " + color + ";\">" + textf + "</span>";
input.push(html);
return input;
});
var input = $.makeArray(arr);
$(element).html(input.join(''));
};
但它不工作非常好,我感覺不良好的代碼,它看起來凌亂。 腳本丟失了不在括號內的內容(「這是我的內容:」)。
任何想法?
您是否計劃嵌套托架塊?像{{}}一些文字{第二}這裏{/第二} {/第一}' – TheBronx 2013-02-22 09:32:40