jQuery更改子项的CSS

时间:2016-03-25 12:06:11

标签: jquery

我有一个简单的jquery片段,它根据选定的类选择一个div,然后对它应用一些CSS

<div class="slc_option wrapper">
<a class="myanchor" title="My Anchor Title 1">My Link 1</a>
</div>
<div class="slc_option wrapper selected">
    <a class="myanchor" title="My Anchor Title 2">My Link 2</a>
</div>
<div class="slc_option wrapper">
    <a class="myanchor" title="My Anchor Title 3">My Link 3</a>
</div>

jsFiddle

jQuery(".slc_option.wrapper.selected").css({"background-color":"red","height":"50px"});

我现在想更改.myanchor类本身的CSS。任何人都有我能看到的例子吗?

3 个答案:

答案 0 :(得分:1)

    jQuery(".slc_option.wrapper.selected .myanchor")
.css({"background-color":"red","height":"50px"});

或者

    jQuery(".slc_option.wrapper.selected>.myanchor")
.css({"background-color":"red","height":"50px"});

答案 1 :(得分:1)

jQuery(".slc_option.wrapper.selected .myanchor").css({"background-color":"red","height":"50px"});

答案 2 :(得分:1)

试试这个:您可以选择类myanchor

的子锚元素
jQuery(".slc_option.wrapper.selected a.myanchor").css({"background-color":"red","height":"50px"});

<强> JSFiddle Link