2015-11-05 5 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 +' nice :) –

+1

Введите здесь свой код. Есть причина, по которой вы должны были помещать пробелы в ссылку Fiddle, и вы обойдетесь с ней, это вызывает у Тони Пони. –

+0

Stack Overflow - это не охота на мусорщика. Пожалуйста, включите соответствующую информацию в вопрос, а не подсказки и подсказки, которые вы хотите, чтобы мы собрали вместе для вас. – David

ответ

0

, где у вас есть

j === f 

должно быть

s[j] === f 

В противном случае вы сравниваете ряд к слову.

+0

Извини, что я плохой. Опечатка. Я уже это сделал. Тем не менее, похоже, это не выход. Обратитесь к обновленному jsfiddle. net/qgkj9pxp –

Смежные вопросы