我想使用jQuery / Java Script搜索和突出显示文本。
示例HTML 1:
<div id="div1"><b>Good</b> <b>Morning</b> </div>
<div id="div2">Good Evening</div>
<div id="div3">Good Morning</div>Searched String = "Good Morning"
当我搜索字符串“早安”时,div1和div3中的内容都应突出显示。即。输出html应该是
<div id="div1"><span class="highlight"><b>Good</b> <b>Morning</b> </span></div>
<div id="div2">Good Evening</div>
<div id="div3"><span class="highlight">Good Morning</span></div>
我使用插件https://raw.github.com/bartaz/sandbox.js/master/jquery.highlight.js将搜索到的内容封装在span中。但只突出显示div3。请帮忙。
答案 0 :(得分:38)
http://jsfiddle.net/UPs3V/291/
var src_str = $("#test").text();
var term = "my text";
term = term.replace(/(\s+)/,"(<[^>]+>)*$1(<[^>]+>)*");
var pattern = new RegExp("("+term+")", "gi");
src_str = src_str.replace(pattern, "<mark>$1</mark>");
src_str = src_str.replace(/(<mark>[^<>]*)((<[^>]+>)+)([^<>]*<\/mark>)/,"$1</mark>$2<mark>$4");
$("#test").html(src_str);
试试这个可以帮助你
答案 1 :(得分:12)
<强>脚本强>
jQuery.fn.highlight = function(pat) {
function innerHighlight(node, pat) {
var skip = 0;
if (node.nodeType == 3) {
var pos = node.data.toUpperCase().indexOf(pat);
if (pos >= 0) {
var spannode = document.createElement('span');
spannode.className = 'highlight';
var middlebit = node.splitText(pos);
var endbit = middlebit.splitText(pat.length);
var middleclone = middlebit.cloneNode(true);
spannode.appendChild(middleclone);
middlebit.parentNode.replaceChild(spannode, middlebit);
skip = 1;
}
}
else if (node.nodeType == 1 && node.childNodes && !/(script|style)/i.test(node.tagName)) {
for (var i = 0; i < node.childNodes.length; ++i) {
i += innerHighlight(node.childNodes[i], pat);
}
}
return skip;
}
return this.each(function() {
innerHighlight(this, pat.toUpperCase());
});
};
jQuery.fn.removeHighlight = function() {
function newNormalize(node) {
for (var i = 0, children = node.childNodes, nodeCount = children.length; i < nodeCount; i++) {
var child = children[i];
if (child.nodeType == 1) {
newNormalize(child);
continue;
}
if (child.nodeType != 3) { continue; }
var next = child.nextSibling;
if (next == null || next.nodeType != 3) { continue; }
var combined_text = child.nodeValue + next.nodeValue;
new_node = node.ownerDocument.createTextNode(combined_text);
node.insertBefore(new_node, child);
node.removeChild(child);
node.removeChild(next);
i--;
nodeCount--;
}
}
return this.find("span.highlight").each(function() {
var thisParent = this.parentNode;
thisParent.replaceChild(this.firstChild, this);
newNormalize(thisParent);
}).end();
};
<强> HTML 强>
Search: <input type="text" id="text-search" />
<p><b>Demo </b> he new edition of KnowlEdge K12 enables your school with flexibility by wholly automating their administrative and academic processes. With IncTech’s solution for K12 schools, you can. We give you an internal infrastructure so you can share school and student information. </p>
<script type="text/javascript">
$(function() {
$('#text-search').bind('keyup change', function(ev) {
// pull in the new value
var searchTerm = $(this).val();
// remove any old highlighted terms
$('body').removeHighlight();
// disable highlighting if empty
if ( searchTerm ) {
// highlight the new term
$('body').highlight( searchTerm );
}
});
});
</script>
答案 2 :(得分:7)
您需要从b
中删除div1
代码,并且为了应用粗体属性,您可以使用css属性font-weight:bold
。
<强> HTML: - 强>
<div id="div1" style="font-weight:bold">Good Morning</div>
<div id="div2">Good Evening</div>
<div id="div3">Good Morning</div>
或
<div id="div1"><b>Good Morning</b></div>
<div id="div2">Good Evening</div>
<div id="div3">Good Morning</div>
<强> JS: - 强>
$("body").highlight("Good Morning");
答案 3 :(得分:0)
使用HTML标签在以上所有答案搜索中尝试使用此方法。
此答案对您在我的项目中实现的完美搜索文本完全有帮助
$connectionToken = "PAT"
$Url1 = "https://dev.azure.com/{OrganizationName}/{ProjectName}/_apis/distributedtask/deploymentgroups/{DeployGroup ID}/deploymentTargetJobRequests?targetId={TargetMachineID}&api-version=6.1-preview.1"
$base64AuthInfo= [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($connectionToken)"))
$json = Invoke-RestMethod -Uri $Url1 -Headers @{authorization = "Basic $base64AuthInfo"} -Method Get
$data = $json | ConvertTo-Json -Depth 100
$data