根据其内容叠加td

时间:2013-11-08 09:06:59

标签: jquery css styles contains

我有一个类似于解决问题的问题,但在我的环境中,解决方案不起作用。

在SharePoint中有一个包含列的表格,当我的内容中的内容像“绿色”时,我想将背景颜色更改为单个td的绿色。

我试过这个

<table>
    <tr>
        <table>
            <tr>
                <th width="200px">Title</th>
                <th width="100px">State</th>
            </tr>
            <tr>
                <td>Hello</td>
                <td>Green</td>
            </tr>
            <tr>
                <td>World</td>
                <td>Red</td>
            </tr>
        </table>
    </tr>
</table>
<script>
    $("td:contains('Green')").css('background-color', '#00FF00');
</script>

My example on JsFiddle

但这不适用于SharePoint table-in-table-structure。为什么? 问题是什么?整个表格是最后一个颜色的颜色。

1 个答案:

答案 0 :(得分:1)

首先包含jQuery文件

之后

编写代码,如

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js" ></script>

<script>
$(document).ready(function(){
$("td:contains('Green')").css('background-color', '#00FF00');
});
</script>