问题描述
$(element)[0] .selectionStart
似乎只适用于textareas。是否有替代非textareas
$(element)[0].selectionStart
seems to only work for textareas. Is there an alternative for non textareas
我正在尝试在标签中的DOM元素中包装字符串(粗体,斜体......)
I'm trying to wrap strings within DOM elements in tags (bold, italic...)
另外,如何撤消包装?
推荐答案
这是一个棘手的问题。您需要对象和。然而,IE完全不同于正确的浏览器。
This is a tricky area. You need the Selection object and DOM Range. However, IE does this all completely differently to proper browsers.
在标签内包装选择通常也是非常重要的:您需要考虑内部的所有文本节点选择。
Wrapping the selection inside tags is also non-trivial in general: you need to consider all the text nodes within the selection.
我的图书馆正常化IE的选择/范围API,并有一个,这可能会在某种程度上帮助你。我还计划编写一个更通用的模块,用于将其他样式应用于选择/范围。
My Rangy library normalizes the Selection/Range API for IE and has a module to apply a CSS class to a selection, which may help you to some degree. I'm also planning to write a more general module for applying other styles to a selection/range.
对于无耻的自我推销感到抱歉,但是写的不多代码你自己我不知道还有什么建议。
Sorry for the shameless self-promotion, but short of writing a lot of code yourself I don't know what else to suggest.
这篇关于selectionStart表示非textarea元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!