如何用IHTMLTxtRange实现文本替换?
我正在使用 IHTMLTxtRange 对象实例来替换当前用户在浏览器中的选择。
今天,我遇到了以下问题。这是我的标记:
<body><p><a href="http://www.google.com">http://www.google.com</a></p></body>
由 IHMLTxtRange 实例(范围)告知的用户选择(html)是:
<a href="http://www.google.com">http://www.google.com</a>
每当我执行 range.replaceHTML("test");
结果是:
<body><p><a href="http://www.google.com">test</a></p></body>
此行为似乎也适用于这样的场景:
<body><p><b>some text</b></p></body>
任何人都可以帮助我制定一致的选择替换策略吗?我的意思是,文本选择按预期工作。
谢谢
2012年10月5日更新:澄清了遇到的问题
现在回来我意识到问题没有正确明确。我希望将整个字符串 http://www.google.com
替换为 test
。相反,锚点的 innerText
被替换,使其标记保持不变。
I'm using an IHTMLTxtRange object instance to replace the current user's selection in the browser.
Today, i came across the following problem. This is my markup:
<body><p><a href="http://www.google.com">http://www.google.com</a></p></body>
The user selection (html), as informed by the IHMLTxtRange instance (range), is:
<a href="http://www.google.com">http://www.google.com</a>
Whenever i execute range.replaceHTML("test");
the result is:
<body><p><a href="http://www.google.com">test</a></p></body>
This behavior seems to apply also to scenarios like this:
<body><p><b>some text</b></p></body>
Can anyone help me to develop a consistent selection replacement strategy? I mean, that text selection works as expected.
Thanks
Update 10/05/2012: Clarified experienced problem
Coming back now I realize that the problem was not explicited correctly. I expected to replace the whole string <a href="http://www.google.com">http://www.google.com</a>
with test
. Instead, the innerText
of the anchor was replaced, leaving it´s markup untouched.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决方案回顾
最后,我通过 Internet Explorer 扩展开发论坛。
作为旁注,来自答案我收到了,在使用
pasteHTML
方法时似乎也会发生这个问题IHTMLTxtRange
对象。甚至还有一个 已关闭的问题 在 Microsoft Connect 网站中描述了此问题。解决方法
根据我得到的答案,我通过检查范围中的当前元素是否是锚点来解决问题:如果是这种情况,则删除当前元素的父元素并获取所需的元素文本被插入到其位置。
我还必须对样式元素进行此检查:
、
和
标记。
请务必查看我回答的问题以获取更多信息。
Solution Review
Finally, I've found the answer to this one through the Internet Explorer Extension Development forums.
As a side note, from the the answer I received, this problem also seems to happen when using the
pasteHTML
method on anIHTMLTxtRange
object. There's even a closed issue describing this problem in the Microsoft Connect website.Workaround
I fixed the problem, based on the answer I got, by checking whether the current element in the range is an anchor: if that's the case, the parent element of the current element is removed and the desired text is inserted in its place.
I had to do this check also for styling elements:
<B>
,<I>
and<FONT>
tags.Be sure to check the question I got answered for more information.