使用鼠标突出显示
我想在屏幕上获取选定的文本(用鼠标突出显示的文本),然后按下按钮时将该文本包装在标签中。我想使用jquery,但如果它可以在另一个框架中完成,那也很好。
我还不知道如何做到这一点,所以任何想法都值得赞赏。我还知道,如果文本跨越多个元素,您可能会遇到问题,因此现在假设突出显示的文本全部包含在标签中
。
谢谢!
I'd like to take the selected text on screen (text highlighted with the mouse) and when a button is pushed wrap that text in a tag. I'd like to use jquery but if it can be done in another framework that would be fine too.
I haven't been able to figure out how to do this yet, so any thoughts are appreciated. Also I know you can run into issues if the text goes across several elements so for now case just assume the text highlighted is all contained in a
tag.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
突出显示所选文本并不一定需要您将其换行。事实上,如果选择的范围跨越多个标签(即没有很好地包围闭合标签),则尝试包装它是很困难的。
这是一个突出显示当前选择而不包装它的答案:Javascript突出显示选定范围按钮< /a>.
他使用 execCommand 让浏览器为您突出显示当前的文档选择。相当甜蜜。
Highlighting the selected text doesn't necessarily require you to wrap it. In fact, trying to wrap it is difficult if the range of the selection spans multiple tags (i.e. doesn't surround nicely closed tags).
Here's an answer that highlights the current selection without wrapping it: Javascript Highlight Selected Range Button.
He uses
execCommand
to let the browser highlight the current document selection for you. Pretty sweet.这里是一篇关于工作的帖子与选定的文本。 getSelection() 方法可用于获取选定的文本,然后您应该能够将该文本替换为标记中包含的文本。
Here is a post on working with selected text. The
getSelection()
method can be used to get the selected text, then you should be able to replace that text with text wrapped in a tag.