国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 語(yǔ)言 > JavaScript > 正文

JS關(guān)鍵字變色實(shí)現(xiàn)思路及代碼

2024-05-06 14:19:34
字體:
供稿:網(wǎng)友
1.替換關(guān)鍵字,對(duì)字體變色
代碼如下:
public static string ReplaceRed(string strtitle, string redkey)
{
if (redkey == "" || redkey == null)
{
return strtitle;
}
else
strtitle = strtitle.Replace(redkey, " <font color='#ff0000'>" + redkey + " </font>");
return strtitle;
}

該方法缺點(diǎn)是:點(diǎn)字符是含大小寫的英文時(shí),變色后統(tǒng)一替換為了關(guān)鍵字的大小寫,體驗(yàn)不好。
2.用正則,CSS背景變色
代碼如下:
protected string HighlightText(string inputText,string searchWord)
{
System.Text.RegularExpressions.Regex expression = new System.Text.RegularExpressions.Regex(searchWord.Replace(" ", "|"), System.Text.RegularExpressions.RegexOptions.IgnoreCase);
return expression.Replace(inputText,new System.Text.RegularExpressions.MatchEvaluator(ReplaceKeywords));
}
public string ReplaceKeywords(System.Text.RegularExpressions.Match m)
{
return "<span class='highlightTxtSearch'>" + m.Value + "</span>";//關(guān)鍵字背景加色
//return "<font color='#ff0000'>" + m.Value + "</font>";//關(guān)鍵字變色
}

該方法可結(jié)合前臺(tái)JS調(diào)用:
代碼如下:
<style type="text/css">
.highlightTxtSearch
{
background-color:Yellow;
}
</style>

代碼如下:
<script type="text/javascript">
$(function () {
$('#tt').datagrid({
url: '@Url.Content("~/Domain/LoadDomainAdmin")',
width: "90%",
height: 400,
fitColumns: true,
nowrap: false,
idField: 'UserID',
pagination: true,
pageNumber: 1,
singleSelect: true,
frozenColumns: [[{ field: 'radio', formatter: function (value, row, index) {
return '<input type="radio" name="rd_action" />';
}
}]],
columns: [[
{ field: 'UserID', title: 'UserID', width: 260, hidden: 'true' },
{ field: 'LoginName', title: '@ViewBag.LoginName', width: 180, align: 'left', formatter: function (data) {
//return "<div class='hiddenFontGommom' style='text-align:left;'>" + data + "</div>";
return GetNewData(data);
}
},
{ field: 'FirstName', title: '@ViewBag.FirstName', width: 120, align: 'left', formatter: function (data) {
//return "<div style='text-align:left;' title=" + data + ">" + data + "</div>";
return GetNewData(data);
// var keyword = $.trim($("#txtInfo").val()) == '@ViewBag.SearchText' ? "" : $.trim($("#txtInfo").val());
// if (keyword == "") {
// return "<div style='text-align:left;' title=" + data + ">" + data + "</div>";
// }
// else {
// var returnData = "";
// $.ajax({
// type: "POST",
// url: '@Url.Content("~/Domain/HighlightText")' + "?inputText=" + data + "&searchWord=" + keyword,
// async: false,
// success: function (newdata) {
// //重新賦值
// returnData = newdata;
// },
// error: function () {
// //不修改returnData值
// }
// });
// return "<div style='text-align:left;' title=" + data + ">" + returnData + "</div>";
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 南通市| 锡林浩特市| 遵义县| 沭阳县| 阿荣旗| 龙南县| 江安县| 越西县| 浙江省| 滕州市| 布尔津县| 岐山县| 民乐县| 丹江口市| 葵青区| 门头沟区| 多伦县| 英超| 吉隆县| 聂拉木县| 湘潭县| 江都市| 宣武区| 汽车| 临沂市| 和平区| 武城县| 普兰县| 类乌齐县| 满城县| 铅山县| 定安县| 麻江县| 万年县| 瓦房店市| 宜兰县| 溧水县| 云浮市| 阿拉善右旗| 布尔津县| 寿宁县|