【问题标题】:How to find selection in HTML document that contains iframe or just frames如何在包含 iframe 或仅包含框架的 HTML 文档中查找选择
【发布时间】:2010-10-22 23:10:02
【问题描述】:

如果文本可能位于其中一个框架(或 iframe)中,有没有办法在 HTML 文档中找到选定的文本?

如果文档没有框架,这很简单:

var text;
if (document.getSelection) {
 // Firefox and friends
 text = document.getSelection();
} else if (document.selection) {
 // IE 
 text = document.selection.createRange();
}
if (text == undefined || text == '') {
 // Iterate over all textarea elements and see if one of them has selection
 var areas = document.getElementsByTagName('textarea');
 for(var i = 0; i = areas.length; i++) {
  if(areas[i].selectionStart != undefined && 
     areas[i].selectionStart != areas[i].selectionEnd){
   text = areas[i].value.substring(areas[i].selectionStart, a[i].selectionEnd);
   break;
  }   
 }   
}
// Now if document has selected text, it's in text

所以这可以跨浏览器工作(虽然不是很漂亮)。

问题在于文档包含框架或 iframe。 框架有自己的文档,所以仅仅使用上面的代码是不够的。 可能会遍历框架树并在其中一个中搜索选定的文本,但是通常框架可以包含来自不同域的内容,因此即使我要遍历搜索中的根文档的所有框架和所有子框架等选定的文本我没有权限访问他们的 HTML,对吧?所以我无法获得他们选择的文本。

是否有一种(简单)可靠的方法可以在网页上找到所选文本,即使该页面包含框架?

谢谢

【问题讨论】:

    标签: javascript html dhtml iframe


    【解决方案1】:

    为了回答我自己的问题,经过更多调查: 因此,如果框架属于不同的域,那么您无能为力,因为您无权访问它们的 dom。但是,在所有框架都在同一个域(例如 gmail)上的常见情况下,只需像树一样迭代主题。下面是实现这一点的代码:

    以下代码用于计算所选文本的字符和单词的书签:

    javascript:(function(){
      // Function: finds selected text on document d.
      // @return the selected text or null
      function f(d){
        var t;
        if (d.getSelection) t = d.getSelection();
        else if(d.selection) t = d.selection.createRange();
        if (t.text != undefined) t = t.text;
        if (!t || t=='') {
          var a = d.getElementsByTagName('textarea');
          for (var i = 0; i < a.length; ++i) {
            if (a[i].selectionStart != undefined && a[i].selectionStart != a[i].selectionEnd) {
              t = a[i].value.substring(a[i].selectionStart, a[i].selectionEnd);
              break;
            }
          }
        }
        return t;
      };
      // Function: finds selected text in document d and frames and subframes of d
      // @return the selected text or null
      function g(d){
        var t;
        try{t = f(d);}catch(e){};
        if (!t || t == '') {
          var fs = d.getElementsByTagName('frame');
          for (var i = 0; i < fs.length; ++i){
            t = g(fs[i].contentDocument);
            if(t && t.toString() != '') break;
          }
          if (!t || t.toString() == '') {
            fs = d.getElementsByTagName('iframe');
            for (var i = 0; i < fs.length; ++i){
              t = g(fs[i].contentDocument);
              if(t && t.toString() != '') break;
            }
          }
        }
        return t;
      };
      var t= g(document);
      if (!t || t == '') alert('please select some text');
      else alert('Chars: '+t.toString().length+'\nWords: '+t.toString().match(/(\S+)/g).length);
    })()
    

    【讨论】:

      【解决方案2】:

      @Ran 对您自己的问题的出色回答。但是,如果 iframe 文档未定义,则该函数将失败。我添加了一个条件来检查这一点,现在它适用于我尝试过的每个网站,包括 Gmail。 if ((!t || t == '') &amp;&amp; d) 再次感谢伟大的代码。

      var getSelectedText = function(){
        // Function: finds selected text on document d.
        // @return the selected text or null
        function f(d){
          var t;
          if (d.getSelection) t = d.getSelection();
          else if(d.selection) t = d.selection.createRange();
          if (t.text != undefined) t = t.text;
          if (!t || t=='') {
            var a = d.getElementsByTagName('textarea');
            for (var i = 0; i < a.length; ++i) {
              if (a[i].selectionStart != undefined && a[i].selectionStart != a[i].selectionEnd) {
                t = a[i].value.substring(a[i].selectionStart, a[i].selectionEnd);
                break;
              }
            }
          }
          return t;
        };
        // Function: finds selected text in document d and frames and subframes of d
        // @return the selected text or null
        function g(d){
          var t;
          try{t = f(d);}catch(e){console.log('ERROR: ',e);};
          if ((!t || t == '') && d){
            var fs = d.getElementsByTagName('frame');
            for (var i = 0; i < fs.length; ++i){
              t = g(fs[i].contentDocument);
              if(t && t.toString() != '') break;
            }
            if (!t || t.toString() == '') {
              fs = d.getElementsByTagName('iframe');
              for (var i = 0; i < fs.length; ++i){
                t = g(fs[i].contentDocument);
                if(t && t.toString() != '') break;
              }
            }
          }
          return t;
        };
        var t= g(document);
        if (!t || t == '') ;
        else return t.toString();
      }
      

      【讨论】:

        猜你喜欢
        • 2019-05-23
        • 1970-01-01
        • 2022-02-15
        • 1970-01-01
        • 1970-01-01
        • 2016-07-01
        • 1970-01-01
        • 2014-12-16
        • 1970-01-01
        相关资源
        最近更新 更多