如何在不同段落的javascript中获取周围环境的选定文本?

时间:2010-12-05 04:23:19

标签: javascript paragraph selectedtext

嗨我有一些问题:

  1. 如果所选单词之前和之后的单词仅限制所选单词周围的20个单词,我可以在所选单词周围得到什么字词?
  2. 如果段落中的单词具有相同的单词,如何获得正确的位置,例如我有这样的句子: foo laa foo doo 然后我选择单词“foo” “,其位置介于 laa doo 之间?
  3. 如何从不同的段落中获取单词?


    例如:
    p1:我喜欢红色衬衫
    第2页:我母亲不喜欢红色衬衫

    我选择的单词是“mother”,我必须在“mother”这个词后面加10个单词“I喜欢红色连衣裙“”我不喜欢红色衬衫。“

  4. 备注:  问题2能够使用nextSibling和previousSibling吗?

    这是我尝试的代码:

    <html>
    <head>
    <script type="text/javascript">
    function getElements(){
      var x = document.getElementsByTagName("body");
      x = x[0].innerHTML;
    
      x = x.replace(/&(lt|gt);/g, function (strMatch, p1){
                return (p1 == "lt")? "<" : ">";});
      var str  = x.replace(/<\/?[^>]+(>|$)/g, "");
    
    
    
      var emailPattern = /[_a-zA-Z0-9\.]+@[\.a-zA-Z0-9]+\.[a-zA-Z]+/gi;
      var urlPattern = /[a-z]+:\/\/[^\s]+/gi;
      var numberOrSymbolPattern = /[0-9\.,!@#\$%\^\&*\(\)`~_\-=\+|\\{}\[\]\s:;<>\?\/]+/gi;
    
      //////alert(str);
    
      var str  = str.replace(emailPattern , " ");
      var str  = str.replace(urlPattern , " ");
      var str  = str.replace(numberOrSymbolPattern , " ");
      //////alert(str);
      var str  = str.replace(/[\n\f\r\t]/g, " ");
    
       var hilangtandabaca = str.replace(/[.!:;'",?]/g," ");
    
       var kataptg = hilangtandabaca;
       //alert(kataptg);
       var kata = new Array();
        kata[0] = " is ";
        kata[1] = " the ";
        kata[3] = " of ";
        kata[4] = " a ";
        kata[5] = " or ";
        kata[6] = " for ";
        kata[7] = " from ";
        kata[8] = " in ";
        kata[9] = " this ";
        kata[10] = " and ";
        kata[11] = " on ";
        kata[12] = " with ";
        kata[13] = " my ";
    
     for(var i=0,regex; i<kata.length; i++){
            var regex = new RegExp(kata[i],"gi");
            kataptg = kataptg.replace(regex," ");
        }
    
       var select = getSelected()+ "";
       alert(select);
       var index = kataptg.indexOf(select);
       //alert("indeks select text:" + index);
       if (index >= 0) {
        var strBefore = "";
        var strAfter = "";
        //var strOri ="";
        //if (index = -1)
        //strOri = kataptg.substr(index);
        //alert(strOri);
        if (index > 0)
        strBefore = kataptg.substr(0, index);
        //alert(strBefore);
        if (index < kataptg.length - 1)
        strAfter = kataptg.substr(index + select.length, kataptg.length - (index + select.length));
        //alert(strAfter);
        alert("Before: " + strBefore + "\nAfter: " + strAfter);
       }
    
    }
    
    
    function getSelected() {
       var userSelection;
       if (window.getSelection) {
          userSelection = window.getSelection();
       } else if (document.selection) {
           userSelection = document.selection.createRange();
       }
       return userSelection;
    }
    
    
    </script>
    </head>                        
    <body>
    <h2>About</h2>
    <p> my email : a@a.a
    <p> my url http://id.wikipedia.org/wiki/URL
    <p> my telepon number = 081330782
    <p>okey In agriculture, the harvest is the process of gathering mature crops from the fields. Reaping is the cutting of grain or pulse for harvest, typically using a scythe, sickle, or reaper.[1] The harvest marks the end of the growing season, or the growing cycle for a particular crop, and this is the focus of seasonal celebrations of on many religions. On smaller farms with minimal mechanization, harvesting is the most labor-intensive activity of the growing season great yeah. !:;'",?</p>
    
    <p>
    <input type="button" onclick="getElements()" value="ambil select text" />
    </p>
    
    </body>
    </html>
    

1 个答案:

答案 0 :(得分:1)

这是JavaScript的innerHTMLsplit()方法的完美示例。您可以循环遍历所有p元素的内容。以下是在第一段中搜索的示例:

contentArray = document.getElementByTagName('p')[0].innerHTML.split(' ')

split(' ')将元素的内容拆分为数组,用空格分隔。 innerHTML是不言自明的。

现在,找到你的话。在这种情况下,indexOf()是您的朋友:

foodex = contentArray.indexOf('foo');
alert('The first occurrence of the string \'foo\' in the text is at word number ' + foodex);

最后,为了得到周围的单词,只需使用数组(如果字符串的出现接近段落的开头或结尾,即不到10个字以内,这将不起作用:

alert('I am the 10th word after \'foo\'' + contentArray[foodex + 10 - 1]);

祝你好运(不保证此代码开箱即用)!