简体   繁体   English

从jQuery中的网格视图HTML获取文本以应用CSS

[英]Get text from grid view HTML in jQuery to apply CSS

I have a asp.net datagrid whose HTML is rendered as below in browser. 我有一个asp.net数据网格,其HTML在浏览器中呈现如下。

<div id="divViewAlldealers" class="">
   <table id="ctl00_cph1_ucMS_grdAllDealers" cellspacing="0" border="1" style="border-collapse:collapse;" rules="all">
     <tbody>
        <tr>
           <td style="background-color: rgb(232, 246, 211);">25 - Vickar Community Chevrolet Ltd</td>
        </tr>
        <tr>
           <td>0 - Jim Gauthier Chevrolet Ltd.</td>
        </tr>
        <tr>
            <td>0 - Murray Chevrolet Ltd.</td>
        </tr>
        <tr>
            <td>0 - Birchwood Chevrolet Buick GMC Ltd.</td>
        </tr>
  </tbody>

Now my goal is I have to give diff background color to the td that is having current customer. 现在我的目标是我必须为拥有当前客户的td提供差异背景颜色。

I know who is the current customer, but how can I find out the customer from grid? 我知道谁是现在的客户,但我怎样才能从网格中找到客户?

Suppose say - Murray Chevrolet Ltd. Note: the list is dynamic and the current customer can come in any place not just as second td. 假设说 - Murray Chevrolet Ltd.注意:该列表是动态的,当前客户可以进入任何地方而不仅仅是第二个td。

I know we can achieve it by using jQuery each() . 我知道我们可以通过使用jQuery each()来实现它。 Is it possible to fix it in jQuery without using each() function? 是否可以在不使用each()函数的情况下在jQuery中修复它?

See this fiddle. 看到这个小提琴。 If you current user is Murray, you can filter all the TD which contains 'Murray' as text and apply back ground color 如果您当前的用户是Murray,您可以将包含'Murray'的所有TD过滤为文本并应用背景颜色

$("tr td:contains('Murray')").each(function(){
  $(this).css("background-color"," rgb(232, 246, 211)");
});

http://jsfiddle.net/KSxLy/ http://jsfiddle.net/KSxLy/

UPDATE UPDATE

$("tr td").filter(function(){
    return $(this).text() === 'San Sarif';
}).css("background-color"," rgb(232, 246, 211)");

JSFIDDLE 的jsfiddle

Better you do add one data attribute to the td which show it as a current customer. 更好的是,您可以向td添加一个数据属性,将其显示为当前客户。 and than work your jQuery selector to change the background-color. 而不是使用你的jQuery选择器来改变背景颜色。

  <table id="ctl00_cph1_ucMS_grdAllDealers" cellspacing="0" border="1" style="border-   collapse:collapse;" rules="all">
 <tbody>
    <tr>
       <td data-currrentCustomer="true">25 - Vickar Community Chevrolet Ltd</td>
    </tr>
    <tr>
       <td>0 - Jim Gauthier Chevrolet Ltd.</td>
    </tr>
    <tr>
        <td>0 - Murray Chevrolet Ltd.</td>
    </tr>
    <tr>
        <td>0 - Birchwood Chevrolet Buick GMC Ltd.</td>
    </tr>
</tbody>
</table>

jQuery jQuery的

$(document).ready(function(){
    $("td[data-currrentCustomer='true']").css({"background-color":"rgb(232, 246,211)"});
});

You can check my Fiddle 你可以查看我的小提琴

You can use the following line of code if you have the current customer name. 如果您有当前客户名称,则可以使用以下代码行。

var custName ="Vickar";
$( "td:contains('"+ custName +"')" ).css( "color", "#FF0000" );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM