如果在屏幕上单击了一个单词 javascript应该显示有单词的句子。 现在这就是我所做的 的jsfiddle。 net / qgkj9pxp /
我有两个功能。首先是找到点击的单词
//<![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 :(得分:0)
你有
的地方j === f
应该是
s[j] === f
否则,您要将数字与单词进行比较。