简体   繁体   中英

jQuery .first-child syntax

As a beginner I struggle with the jQuery syntax. I realize that the following code is NOT selecting the first-child (the div).

My question: Why is the syntax wrong and how should I select the div in the previous anchor element? Please can/will someone answer this simple questions for me or put me in in the right direction?

(I did search but I just can't find the answer.) Manny thanks in advance!

function hilight(a) {
$('a').prev().first-child.css({"backgroundColor":"#ffffff","color":"#000000"}); }

<div>
<a href="#" class="bttn"><div class="bttn">Button</div></a>
<a href="#" class="image" onmouseover="hilight(this)" onmouseout="normal(this)">
    <img src="imgage.png"/>
</a></div>

There are 2 problems

function hilight(a) {
    //use a as a variable reference & use .children() to find the first child
    $(a).prev().children(':first-child').css({
        "backgroundColor": "#ffffff",
        "color": "#000000"
    });
}

Since you are using jQuery, prefer to use jQuery event handlers instead of using inlined handlers.

Demo: Fiddle

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