如何在jquery中更改单词的颜色(How to change a colour of a word in jquery)

编程入门 行业动态 更新时间:2024-10-27 17:21:46
如何在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; }

更多推荐

本文发布于:2023-07-25 23:07:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1267568.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单词   颜色   如何在   jquery   word

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!