如何在jquery中更改单词的颜色(How to change a colour of a word in jquery)
我将如何能够选择一个单词并更改该单词的颜色?
例如用样式添加一个范围并改变字体颜色。
请有人带领我走正确的道路。
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>谢谢,
How would i be able to select a word and change the colour of that one word?
e.g. add a span with style and chage the font colour.
Can someone lead me down the right path please.
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>Thank you,
最满意答案
最简单的方法是在div中动态添加跨度,并更改当前跨度的颜色。 这是基本的想法:
$(".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; }The easiest way would be to dynamically add spans with the individual in the div and just change the color of the current span. Here's the basic idea:
$(".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; }更多推荐
发布评论