我有一个JQuery UI对话框,其中显示对表单提交中的输入所做的更改。我希望“新”一词使用不同的颜色,但它只会更改第一次出现的颜色。
为什么replace()函数对换行有效,但对CSS更改无效?
var i;
for(i = 0; i < changed_arr.length; i++) {
var oldValue = changed_arr[i].oldValue;
var newValue = changed_arr[i].newValue;
str += changed_arr[i].key + ": Old - " + oldValue + ", New - " + newValue + "\n";
}
error_text = $('#dialog_confirm p').text("Please confirm your update\nbefore proceeding.\n\nChanges for <?= $edit_domain;?>: \n\n" + str);
//The new line works multiple times, but not the css change
error_text.html(error_text.html().replace(/\n/g, '<br/>').replace('New', '<span style="color:yellow;">New</span>'));
$('#dialog_confirm').dialog('open');
}
答案 0 :(得分:0)
您需要为“新建”添加g
(全局)和m
(多行)修饰符:
replace(/New/gm, '<span style="color:yellow;">New</span>')
它应该替换所有与换行符类似的内容。
示例: