非文本区域元素的选择开始

发布于 2024-10-08 10:57:56 字数 135 浏览 0 评论 0原文

$(element)[0].selectionStart 似乎仅适用于文本区域。对于非文本区域,是否有替代方案

我试图将字符串包装在标签中的 DOM 元素中(粗体、斜体...)

另外,如何撤消包装?

$(element)[0].selectionStart seems to only work for textareas. Is there an alternative for non textareas

I'm trying to wrap strings within DOM elements in tags (bold, italic...)

Also, how do I undo the wrapping?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情深如许 2024-10-15 10:57:56

这是一个棘手的领域。您需要 Selection 对象和 DOM 范围。然而,IE 的做法与普通浏览器完全不同。

一般来说,将选择内容包装在标签内也很重要:您需要考虑选择内容中的所有文本节点。

我的 Rangy 库规范了 IE 的 Selection/Range API,并具有 将 CSS 类应用于所选内容的模块,这可能会在某种程度上帮助您。我还计划编写一个更通用的模块,用于将其他样式应用于选择/范围。

很抱歉无耻的自我推销,但由于自己编写了大量代码,我不知道还有什么建议。

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.

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.

ぃ弥猫深巷。 2024-10-15 10:57:56

使用 jQuery fieldSelection 插件
http://plugins.jquery.com/project/fieldselection

我在我的脚本中使用它来剥离粘贴标签,看起来效果很好。
http://blog.brokenbytes.info/2010/ 09/stripping-tags-on-paste-using-jquery/

use the jQuery fieldSelection plugin
http://plugins.jquery.com/project/fieldselection

i use it in my script that strips tags on paste and it seems to work great.
http://blog.brokenbytes.info/2010/09/stripping-tags-on-paste-using-jquery/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文