我是jquery的新手我有一个简单的html textarea,我想计算文本区域中的特定单词并显示该计数并同时突出显示该文本。
$(document).ready(function() {
$(".text").on('input',function(){
var a= $(".text").val();
//var a="i m running";
/*if(a==="nokia"){
alert("nokia found");
}
else{
alert("not found");
}*/
var pattern = /nokia/;
var pattern1 = /samsung/;
var pattern2 = /iphone/;
var pattern3 = /qmobile/;
//returns true or false...
var exists = pattern.test(a);
var exists1 = pattern1.test(a);
var exists2 = pattern2.test(a);
var exists3 = pattern3.test(a);
if(exists){
//true statement, do whatever
//alert("nokia");
$(".nokia").css("background-color", "green");
}
else{
//false statement..do whatever
//alert("not nokia");
$(".nokia").css("background-color", "red");
}
if(exists1){
//true statement, do whatever
//alert("samsung found");
$(".samsung").css("background-color", "green");
}
else{
//false statement..do whatever
// alert("not samsung found");
$(".samsung").css("background-color", "red");
}
if(exists2){
//true statement, do whatever
//alert("samsung found");
$(".iphone").css("background-color", "green");
}
else{
//false statement..do whatever
//alert("not samsung found");
$(".iphone").css("background-color", "red");
}
if(exists3){
//true statement, do whatever
// alert("samsung found");
$(".qmobile").css("background-color", "green");
}
else{
//false statement..do whatever
//alert("not samsung found");
$(".qmobile").css("background-color", "red");
}
});
counter = function() {
var value = $('#text').val();
if (value.length == 0) {
$('#wordCount').html(0);
$('#totalChars').html(0);
$('#charCount').html(0);
$('#charCountNoSpace').html(0);
return;
}
var regex = /\s+/gi;
var wordCount = value.trim().replace(regex, ' ').split(' ').length;
$('#wordCount').html(wordCount);
};
$(document).ready(function() {
$('#text').keydown(counter);
});
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<textarea id='text' style="width: 50%;height: 20%;" type="text" class="text"/></textarea>
<button name="click" class="btn"/>click</button>
<div id="result">
Words: <span id="wordCount">0</span><br/>
</div>
<ul>
<li class="nokia">nokia</li>
<li class="samsung">samsung</li>
<li class="iphone">iphone</li>
<li class="qmobile">qmobile</li>
</ul>
&#13;