如何更改锚<a> style when it&#39;s in table cell using jquery

时间:2015-09-18 19:55:44

标签: javascript jquery html css

I have following problem need to change style of which is in table cell using jQuery. The change is dependable on value in other cell. I have following table:

                             <table id="table">
                                <tr>
                                    <td>
                                        <a href="www.google.pl">YYYYYYYYY</a></td>
                                    <td>HHHHHHH<td>
                                </tr>
                                <tr>
                                    <td>Jedi Armor1</td>
                                    <td>HHHHHHH</td>
                                </tr>
                                <tr>
                                    <td>Jedi Armor2</td>
                                    <td>HHHHHHH</td>
                                </tr>
                            </table>

jQuery method:

            $(document).ready(function() {

                $('#table tr td').each(function(){
                     var textValue = $(this).text();
                        if(textValue == 'HHHHHHH'){
                        // add css class or any manipulation to your dom.
                    $(this).parent().css('color','red');
                    $(this).parent().children('td a').css('color','red');
                    };
                });

            // koniec funkcji hover
        });

The problem is how to change style??

2 个答案:

答案 0 :(得分:0)

如果您将children更改为find,则您的代码将有效。

请参阅此fiddle了解演示

答案 1 :(得分:0)

$(this).parent().children('td a') 看不到2级

变化:

$(this).parent().find('a');

$(this).parent().addClass('has_H');

但最简单的方法是只在行中添加一个类并使用css调整整行的样式。切换类通常比撤消内联css

更容易
tr.has_H, tr.has_H a{
    color:red;
}

CSS

div