2015-11-05 119 views
-5

如果在屏幕上点击一个词 javascript应显示包含该词的句子。 现在这就是我所做的 jsfiddle。 net/qgkj9pxp/Javascript调用功能不起作用

我有两个功能。首先是要找到这个词点击这是

//<![CDATA[ 
 
$(window).load(function() { 
 
    $("body").click(function() { 
 
     // Gets clicked on word (or selected text if text is selected) 
 
     var t = ''; 
 
     var nstr = ''; 
 
     if (window.getSelection && (sel = window.getSelection()).modify) { 
 
      // Webkit, Gecko 
 
      var s = window.getSelection(); 
 
      if (s.isCollapsed) { 
 
       s.modify('move', 'forward', 'character'); 
 
       s.modify('move', 'backward', 'word'); 
 
       s.modify('extend', 'forward', 'word'); 
 
       t = s.toString(); 
 
       s.modify('move', 'forward', 'character'); //clear selection 
 
      } else { 
 
       t = s.toString(); 
 
      } 
 
     } else if ((sel = document.selection) && sel.type != "Control") { 
 
      // IE 4+ 
 
      var textRange = sel.createRange(); 
 
      if (!textRange.text) { 
 
       textRange.expand("word"); 
 
      } 
 
      // Remove trailing spaces 
 
      while (/\s$/.test(textRange.text)) { 
 
       textRange.moveEnd("character", -1); 
 
      } 
 
      t = textRange.text; 
 

 
     } 
 

 
     t = t.replace(/[^\w\s]|_/g, "").replace(/\s+/g, " "); 
 
     isThere(t); 
 

 

 
    }); 
 
    
 
}); 
 

 
//]]>

和由第一函数调用第二函数来检查这句话的点击单词。

function isThere(f) { 
 
    var str = "Big data is a broad term for data sets so large or complex that traditional data processing applications are inadequate. Challenges include analysis, capture, data curation, search, sharing, storage, transfer, visualization, and information privacy. The term often refers simply to the use of predictive analytics or other certain advanced methods to extract value from data, and seldom to a particular size of data set. Accuracy in big data may lead to more confident decision making. And better decisions can mean greater operational efficiency, cost reduction and reduced risk. Analysis of data sets can find new correlations, to spot business trends, prevent diseases, combat crime and so on.Scientists, business executives, practitioners of media and advertising and governments alike regularly meet difficulties with large data sets in areas including Internet search, finance and business informatics. Scientists encounter limitations in e-Science work, including meteorology, genomics, connectomics, complex physics simulations and biological and environmental research. Work with big data is necessarily uncommon; most analysis is of PC size data, on a desktop PC or notebook that can handle the available data set."; 
 

 
    var spli = str.split('.'); 
 
    var len = spli.length; 
 
    var i, j; 
 
    for (i = 0; i < len; i++) { 
 
     var s = spli[i].split(" "); 
 
     for (j = 0; j < s.length; j++) { 
 
      if (s[j] === f) 
 
       alert(spli[i]); 
 
     } 
 
    } 
 
}

请帮助我,并告诉我,为什么第二个功能无法正常工作?

+0

'// IE 4 +'不错:) :) –

+1

把你的代码放在这里。有一个原因,你必须在小提琴链接中放置空格,并且你绕着小马托尼小马。 –

+0

堆栈溢出不是寻找清道夫。请在问题本身中包含相关信息,而不是您希望我们拼凑在一起的线索和提示。 – David

回答

0

,你必须

j === f 

应该

s[j] === f 

否则你一些比较一句话。

+0

对不起,我的坏。错字。我已经做到了。仍然不会产生输出。参考更新的jsfiddle。 net/qgkj9pxp –