简体   繁体   中英

Add class to li-Element

I´m trying to add a class or id to the last list element. But it do not work. (with no error message):

<div id="preistabelle">
 <ul class="pricing-cols">
   <li class=pricing-col three-table "></li>
   <li class=pricing-col three-table featured plan "></li>
   <li class=pricing-col three-table "></li>
</div>


 <script>
 $(".pricing-cols ul li:last-child").addClass("last");
 </script>

I fixed it for you. You had a lot errors in your html code.

HTML:

<div id="preistabelle">
    <ul class="pricing-cols">
        <li class="pricing-col three-table "></li>
        <li class="pricing-col three-table featured plan "></li>
        <li class="pricing-col three-table ">test</li>
    </ul>
</div>

Javascript:

$(function () {
    $(".pricing-cols li:last-child").addClass("last");
});

CSS (only for testing)

.last {
    background-color: black;
}

Working demo: http://jsfiddle.net/2yZJJ/

$(".pricing-cols ul li:last-child").addClass("last");

当.pricing-cols是ul时,这会在.pricing-cols中选择一个ul,更改为:

$(".pricing-cols li:last-child").addClass("last");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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