如何使用 jQuery 将 css 应用于 savedRange 文本选择? [英] How can I use jQuery to apply css to savedRange text selection?

查看:23
本文介绍了如何使用 jQuery 将 css 应用于 savedRange 文本选择?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Is it possible to apply css or wrap tags around savedRange text selection. I'm using jquery.

this doesn't work:

$(savedRange).css(...);
$(savedRange).wrap(...);

解决方案

If by "savedRange" you mean something like this:

selection = window.getSelection()
savedRange = selection.getRangeAt(0)

Then you will have to create a wrapper for the range first, like so:

wrapper = document.createElement('span')
savedRange.surroundContents(wrapper)
selection.selectAllChildren(wrapper)

You can then apply style:

$(wrapper).css(...)

这篇关于如何使用 jQuery 将 css 应用于 savedRange 文本选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆