【问题标题】:Finding Text in <td>, then conditionally changing text in another <td>在 <td> 中查找文本,然后有条件地更改另一个 <td> 中的文本
【发布时间】:2012-07-17 06:47:19
【问题描述】:

我正在使用asp:Repeater,我需要一些帮助。在我的表格中,我有一个 &lt;td> 我想更改颜色,当且仅当它右侧的 &lt;td&gt; 包含某个文本。

代码如下:

 <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>

在这种情况下,如果包含“certainText”,我想给背景颜色。对于转发器中的每个项目都必须这样做,这对我来说也是一个挑战。

我在 jsfiddle 上找到了this,但在我的解决方案中使用它时没有任何运气。

感谢您的帮助!

【问题讨论】:

    标签: javascript jquery asp.net html


    【解决方案1】:
    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'});
      });
    });
    

    【讨论】:

    • 您可能希望隐藏
      直到脚本完成,这样屏幕不会在用户电脑/连接速度较慢时闪烁/变化。
    【解决方案2】:

    试试这个:

    $(document).ready(function()
    {
      $('td').each(function(index) 
      {
        if($(this).text().indexOf("certainText") >= 0 )
        {
          $(this).css('background-color','red');
        }
      });
    });
    

    【讨论】:

      【解决方案3】:

      试试这个例子:

      http://www.c-sharpcorner.com/blogs/7592/change-the-forecolor-of-an-item-in-a-repeater-based-on-the-s.aspx

      您可以在您使用的 sql 适配器类型上使用 foreach,并验证调用 IF 的方法

      dt.NewRow();

      ,在示例中,因此验证每个字段并为其分配您想要的颜色

      【讨论】:

        【解决方案4】:

        我不知道您的 DataSource 是什么,但您可以更改下面的代码以使用 ASP.NET 方式:

        标记:

         <asp:Repeater ID="myRepeater" runat="server"                 
                   OnItemDataBound="myRepeater_ItemDataBound">
          ................
         <ItemTemplate>
                <tr>
                    <td class="myClass" id="td1" runat="server">
                    </td>
                    <td class="changeMyColor!" id="td2" runat="server">
                    </td>
            <td class="lookAtMe">
             <asp:label id="lookAtMe" Text="<%#Eval("LookAtMe")%>" />
            </td>
         </ItemTemplate>
        

        代码隐藏:

          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");
                 }
        
              }
           }    
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-04-22
          • 1970-01-01
          • 2019-08-15
          • 1970-01-01
          • 2014-02-15
          • 2017-12-19
          • 2012-07-26
          • 1970-01-01
          相关资源
          最近更新 更多