Javascript 在中查找文本,然后有条件地更改另一中的文本
我正在使用asp:Repeater,我需要一些帮助。在我的表中,我有一个我想改变颜色的,当且仅当,它右边的包含一个确定的文本 下面是代码的样子:Javascript 在中查找文本,然后有条件地更改另一中的文本,javascript,jquery,asp.net,html,Javascript,Jquery,Asp.net,Html,我正在使用asp:Repeater,我需要一些帮助。在我的表中,我有一个我想改变颜色的,当且仅当,它右边的包含一个确定的文本 下面是代码的样子: <asp:Repeater ID="myRepeater" runat="server"> <HeaderTemplate> <div id="myDiv"> <table id="table">
<asp:Repeater ID="myRepeater" runat="server">
<HeaderTemplate>
<div id="myDiv">
<table id="table">
<thead>
<tr>
<th class="class">
Row 1
</th>
<th class="class">
Row 2
</th>
<th class="class">
Row 3
</th>
</tr>
</thead>
<tbody>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td class="myClass">
</td>
<td class="changeMyColor!">
</td>
<td class="lookAtMe">
certainText
</td>
</ItemTemplate>
在本例中,我想为是否包含确定文本提供背景色。这对我来说也是一个挑战,这必须发生在中继器中的每一个项目上
我在JSFIDLE上找到了它,但在我的解决方案中没有使用它来实现它
谢谢你的帮助 试试这个:
$(document).ready(function()
{
$('td').each(function(index)
{
if($(this).text().indexOf("certainText") >= 0 )
{
$(this).css('background-color','red');
}
});
});
试试这个:
$(document).ready(function()
{
$('td').each(function(index)
{
if($(this).text().indexOf("certainText") >= 0 )
{
$(this).css('background-color','red');
}
});
});
试试这个例子:
您可以对所使用的sql适配器类型使用foreach,并验证用于调用
纽罗博士
,它位于示例中,因此验证每个字段并为其指定所需的颜色。请尝试以下示例:
您可以对所使用的sql适配器类型使用foreach,并验证用于调用
纽罗博士
,这在示例中,因此验证每个字段并为其指定所需的颜色我不知道您的数据源是什么,但您可以通过ASP.NET方式更改以下代码:
标记:
我不知道您的数据源是什么,但您可以更改以下代码,以ASP.NET方式执行此操作:
标记:
您可能希望在脚本完成之前隐藏,以便在pc/连接速度较慢的用户上屏幕不会闪烁/更改。您可能希望在脚本完成之前隐藏,以便在pc/连接速度较慢的用户上屏幕不会闪烁/更改。
protected void myRepeater_ItemDataBound(Object Sender, RepeaterItemEventArgs e) {
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
YourDataObject data = (YourDataObject)e.Item.DataItem;
HtmlGenericControl td1 = e.Item.FindControl("td1") as HtmlGenericControl;
HtmlGenericControl td1 = e.Item.FindControl("td2") as HtmlGenericControl;
if (data.YourProperty == "CertainText") {
td1.Attributes.Add("class","whateverClass");
td2.Attributes.Add("class","whateverClass2");
}
}
}
jQuery(document).ready(function($) {
//Find <td>'s in your table id="table" which contains text 'certainText'
$('#table tbody td.lookAtMe:contains(certainText)').each(function() {
//You said the test was for the <td> to the right of it
//which means .prev() will return the <td> to the left
//select that <td> and change its color
$(this).prev().css({'background-color':'#a90000'});
});
});