Jquery - 在字符串中查找一个单词并用 <span> 标签包装它
Jquery - Find a word in a string and wrap it with <span> tag
我需要编写一个函数,将一个单词作为arg,在字符串中找到它,并仅使用jQuery/JS突出显示该单词。
jsfiddle.net/xzh2qmcd/
我已经在这里得到了这个词并得到了索引,但我不知道如何仅在这个词出现的地方添加一个 span 标签。
任何帮助我们都很棒,我在这里检查了一些其他问题,但不明白我应该怎么做。
你需要这样做:
var txt = textDiv.replace(new RegExp(searchInput, 'gi'), function(str) {
return "<span class='highlight'>" + str + "</span>"
});
或
var txt = textDiv.replace(
new RegExp(searchInput, 'gi'),
"<span class='highlight'>$&</span>");
gi
->
所有不区分大小写的匹配文本
$(document).ready(function() {
// globals
var searchInput;
var textDiv;
$('.searchBtn').click(function(event) {
event.preventDefault();
// set the values of the search and the text
searchInput = $('.searchInput').val();
textDiv = $('.textDiv').text();
var reg = new RegExp(searchInput, 'gi');
var txt = textDiv.replace(reg, function(str) {
return "<span class='highlight'>" + str + "</span>"
});
$('.textDiv').html(txt);
});
});
.textDiv {
height: 100px;
width: 500px;
text-align: center;
padding: 20px;
margin: 0 auto;
}
.highlight {
background: yellow;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="container">
<div class="main">
<div class="searchDiv">
<h3> Mask A Word In The Text: </h3>
<label>
<input class="searchInput" type="text" value = "iS"/> </label>
<button class="searchBtn" type="button"> Search </button>
</div>
<div class="textDiv">
Bastille Day is the common name given in English-speaking countries to the French National Day, which is celebrated on 14 July each year. In France, it is formally called La fête nationale (French pronunciation: [la fɛːt nasjɔnal]; The National Celebration) IS Is
and commonly Le quatorze juillet. (French pronunciation: [lə katɔʁz(ə) ʒɥijɛ]; the fourteenth of July).
</div>
</div>
</div>
$(document).ready(function() {
// globals
var searchInput;
var textDiv;
$('.searchBtn').click(function(event) {
event.preventDefault();
// set the values of the search and the text
searchInput = $('.searchInput').val();
textOfDiv = $('.textDiv').text();
// search for the input in the paragraph
if (textOfDiv.indexOf(searchInput) >= 0) {
console.log("match");
var newText = textOfDiv.split(searchInput).join('<span class="highlight">' + searchInput + '</span>' );
$('.textDiv').html(newText);
}
else {
console.log("no match");
}
});
});
.textDiv {
height:100px;
width:500px;
text-align:center;
padding:20px;
margin:0 auto;
}
.highlight {
background:red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
<div class="main">
<div class="searchDiv">
<h3> Mask A Word In The Text: </h3>
<label> <input class="searchInput" type="text" /> </label>
<button class="searchBtn" type="button"> Search </button>
</div>
<div class="textDiv">
Bastille Day is the common name given in
English-speaking countries to the French National Day,
which is celebrated on 14 July each year. In France,
it is formally called La fête nationale (French pronunciation: [la fɛːt nasjɔnal]; The National Celebration)
and commonly Le quatorze juillet.
(French pronunciation: [lə katɔʁz(ə) ʒɥijɛ]; the fourteenth of July).
</div>
</div>
</div>
$(document).ready(function() {
// globals
var searchInput;
var textDiv;
$('.searchBtn').click(function(event) {
event.preventDefault();
// set the values of the search and the text
searchInput = $('.searchInput').val();
textOfDiv = $('.textDiv').text();
// search for the input in the paragraph
if (textOfDiv.toLowerCase().indexOf(searchInput) >= 0) {
console.log("match");
var newText = textOfDiv.split(searchInput).join('<span class="highlight">' + searchInput + '</span>' );
$('.textDiv').html(newText);
}
else {
console.log("no match");
}
});
});
.textDiv {
height:100px;
width:500px;
text-align:center;
padding:20px;
margin:0 auto;
}
.highlight {
background:red;
}
<div class="container">
<div class="main">
<div class="searchDiv">
<h3> Mask A Word In The Text: </h3>
<label> <input class="searchInput" type="text" /> </label>
<button class="searchBtn" type="button"> Search </button>
</div>
<div class="textDiv">
Bastille Day is the common name given in
English-speaking countries to the French National Day,
which is celebrated on 14 July each year. In France,
it is formally called La fête nationale (French pronunciation: [la fɛːt nasjɔnal]; The National Celebration)
and commonly Le quatorze juillet.
(French pronunciation: [lə katɔʁz(ə) ʒɥijɛ]; the fourteenth of July).
</div>
</div>
</div>
您可以使用以下代码实现所需的目标。
我已经更新了我的答案以提供完整的单词匹配。我想你只会寻找那个。
示例:如果您搜索国家,那么它不应该为国家单词着色,如果您不进行完整的单词搜索,就会发生这种情况。
// search for the input in the paragraph
if (textDiv.toLowerCase().indexOf(searchInput.toLowerCase()) >= 0) {
console.log("match");
textDiv = textDiv.replace(new RegExp('''b('+searchInput+')''b', 'gi'),'<span class="highlight">$1</span>');
$('.textDiv').html(textDiv);
小提琴:http://jsfiddle.net/xzh2qmcd/3/
相关文章:
- 用类javascript包装span标记中字符串中的字符索引
- 如何将所有文本包装成唯一的span标记
- 如何将新输入的文本包装到下面已经存在的span标记中
- JavaScript,Regexp - 将颜色值包装在 span 中
- JavaScript:如何用包含唯一id的span标记将字符串中的每个单词包装起来
- 如何用span包装两个动态创建的元素
- 如何将元素的每个单词包装在 span 标记中
- 用prototype.js在span中包装html内容-这可能吗?
- js用span包装每个h3标签
- 如何修改文本节点的HTML(类型3)/如何将其包装在HTML span中
- 试图添加两个数字,但它们被包装在span标签中
- 将每个单词的第一个字母包装在span标签- javascript中
- 如何将span元素添加到img,然后用链接包装所有内容?
- 将javascript生成的html结构包装在具有多个端点实例的span中
- 查找用span包装的文本框值和属性
- 阻止Chrome包装joind<p>其中<span>
- 获取某些文本替换它并将其包装在span中
- 如果元素包含这些单词中的任何一个,则将该单词包装在 span 中
- 在javascript中,在单词边界的span标签中包装每个单词的正则表达式
- 在span标签javascript中包装数字