我如何才能选择一个词并更改该词的颜色?
例如添加带有样式的跨度并更改字体颜色。
有人能引导我走上正确的道路吗。
Jquery
function edit_addon (div_id) {
$("#"+div_id).attr ('contentEditable', true)
.css ('color','#F00')
.css ('cursor','Text')
;
}
HTML
<div id="34" ondblclick="javascript:edit_addon(34)">Editable Text</div>
谢谢,
最佳答案
最简单的方法是在 div 中动态添加带有个人的 span 并仅更改当前 span 的颜色。这是基本的想法:
$(".Editable").dblclick( function () {
var words = $(this).text().split(" ");
var result = "";
for (var i=0; i<words.length; i++) {
result.append("<span class='word'>" + words[i] + "</span>");
}
$(this).innerHTML = result;
}
#CSS
span.word:hover { color: #0f0; }
关于javascript - 如何在jquery中更改单词的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3663305/