Internet Explorer 中范围/选择中的原始标记

发布于 2024-08-09 02:09:00 字数 1156 浏览 6 评论 0原文

我正在通过此功能提取用户选择的内容:

 function getSelectionHTML() {

  var userSelection;
  if (window.getSelection)
  {

   // W3C Ranges
   userSelection = document.getElementById('axPage').contentWindow.getSelection();
   // Get the range:
   if (userSelection.getRangeAt)
    var range = userSelection.getRangeAt (0);
   else
   {
    var range = document.createRange ();
     range.setStart (userSelection.anchorNode, userSelection.anchorOffset);
     range.setEnd (userSelection.focusNode, userSelection.focusOffset);
   }

   var clonedSelection = range.cloneContents ();
   var div = document.createElement ('div');
   div.appendChild (clonedSelection);
   return div.innerHTML;

  }
  else if (document.selection)
  {
userSelection = self.frames['axPage'].document.selection.createRange();
   return userSelection.htmlText;
  }
  else
  {

   return '';

  }

  };

提取工作正常,但我似乎没有找到一种方法来获取 Internet Explorer 中所选文本的原始标记。有什么方法可以像 Firefox 一样获取 DocumentFragment 吗?我知道这两个系统的不同处理和方法,但也许有人在 javascript / ECMA 中找到了一种工作方法来摆脱 HREF 中讨厌的 ...jQueryXXXXXXXXXXXXX="12" 插件以及令人痛苦的大写标签和属性名称。

再次强调:选择准确的标记至关重要。

如有任何帮助,我们将不胜感激,谢谢。

I am extracting content selected by the user via this function:

 function getSelectionHTML() {

  var userSelection;
  if (window.getSelection)
  {

   // W3C Ranges
   userSelection = document.getElementById('axPage').contentWindow.getSelection();
   // Get the range:
   if (userSelection.getRangeAt)
    var range = userSelection.getRangeAt (0);
   else
   {
    var range = document.createRange ();
     range.setStart (userSelection.anchorNode, userSelection.anchorOffset);
     range.setEnd (userSelection.focusNode, userSelection.focusOffset);
   }

   var clonedSelection = range.cloneContents ();
   var div = document.createElement ('div');
   div.appendChild (clonedSelection);
   return div.innerHTML;

  }
  else if (document.selection)
  {
userSelection = self.frames['axPage'].document.selection.createRange();
   return userSelection.htmlText;
  }
  else
  {

   return '';

  }

  };

Extraction is working fine, but I do not seem to find a way to get the original markup of the selected text in Internet Explorer. Is there any way to get a DocumentFragment like in Firefox? I know about the different handling and approach of both systems, but maybe someone has found a working approach in javascript / ECMA to get rid of the nasty ...jQueryXXXXXXXXXXXXX="12" addons in HREFs and that painful uppercase tag- and attributenames.

Again: it is essential to get the exact markup selected.

Any help is appreciated, thank you.

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

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

发布评论

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

评论(2

落花浅忆 2024-08-16 02:09:00

谢谢你的帮助,我终于明白了。如果您需要在 Internet Explorer 中获取带有小写标签的选定文本,请尝试此 http:// codingforums.com/showthread.php?t=148450 函数,如果你使用 jquery 并且有讨厌的小 .jQueryXXXXXXXXXXXXX="12" 13, 14 就在每个 href=""-ending " 之后,你的解决方案是复制将您的标记添加到 div 并读取其 insideHTML

希望有所帮助。

Thank you for your help, I finally figured it out. If you need to get selected text in Internet Explorer with tags to lower-case try this http://codingforums.com/showthread.php?t=148450 function and if you use jquery and have nasty little .jQueryXXXXXXXXXXXXX="12" 13, 14 right after every single href=""-closing " your solution is to copy your markup to a div and read its innerHTML.

Hope that helps

赤濁 2024-08-16 02:09:00

在 IE 中,您唯一的选择是编写代码以在选择边界点之间遍历 DOM,或者像您一样使用选择 TextRange 中的 htmlText 。第一个选项会非常复杂。我不太明白第二种方法的问题是什么:您能否更准确地说明或者举一个例子,说明从 htmlText 获得的 HTML 有什么问题?

Your only options in IE are writing code to walk the DOM between the selection boundary points or using htmlText from the selection TextRange as you are doing. The first option will be pretty involved. I don't really understand what your problem is with the second approach: could you be more precise or give an example of what's wrong with the HTML you get from htmlText?

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