0

the html code :

<ul id="top10">
 <li class="test">
    <div class="first">
         <a  href="#"><img src="01.jpg"></a>
    </div>
     <div class="last">
 <a href="#">example one</a>
    </div>
</li>

 <li class="test">
    <div class="first">
         <a  href="#"><img src="01.jpg"></a>
    </div>
     <div class="last">
 <a href="#">example one</a>
    </div>
</li>

 <li class="test">
    <div class="first">
         <a  href="#"><img src="01.jpg"></a>
    </div>
     <div class="last">

example one example one

my jquery code:

$(document).ready(function(){
    $("div.first img").hide();
    $("ul li:first img").show();
    $("#top li").hover(function(){
        $(this).find("img").show();
    });
});

but it doesn't work.

i want to get: when open the page. the first image is show, the rest are all hidden. when the mouse hovers on one item title in it. it shows its image. all others are hidden.

PS: i am sorry.it's ok now. i made a mistake to the ID.but i know my jquery code is bad. is there a better way to get it

3
  • What does "it doesn't work" mean exactly? Is the hover function triggered? Does "find" find anything? Commented Oct 21, 2011 at 9:36
  • Where is #top? Did you mean #top10, which is the id of the ul? Commented Oct 21, 2011 at 9:36
  • Where is the element with the ID #top that you are referencing? Commented Oct 21, 2011 at 9:36

2 Answers 2

2

Specify the correct ID: top10 instead of top. Also, your first two lines can be merged using :gt(0):

$(document).ready(function(){
    $("div.first:gt(0) img").hide(); //Every image after the first (index 0) img
    $("#top10 li").hover(function(){ //top 10
        $(this).find("img").show();
    });
});

Fiddle: http://jsfiddle.net/BfHwL/

Sign up to request clarification or add additional context in comments.

5 Comments

thank U,now i want to make the example one text's font-weight to bold, when the mouse hove on the li. i add this line, $(this).children("div.last a").css("font-weight","bold");but there is no effect. ps: at first i set the font-weight:normal by css
@enjoylife .children() does only select direct childs of the current selector. You should at least use $("div.lasts a", this).css("font-weight", "bold"). I recommend to use class names instead of direct styles, because it's easier to update class names than style attributes: .addClass("test") and .removeClass("test") + CSS: .test{font-weight:bold;}
thank u. but when i used $("#top10 li").removeClass("fontweight") $("#top10 li").hover(function(){ $(".firsttop img").hide(); $(this).addClass('fontweight').find("img").show(); }); when i moveout the li. the text also bold. and the first one also bold. i want to get when the mouse hover on the image, the example one text will be bold and the others don't. the default state is using bold to the first example one text.
@enjoylife Inspect your HTML: You've got <b></b> tags, which cause text to be boldfaced. See this Fiddle for the solution. See jsfiddle.net/BfHwL/2 for the pure CSS solution.
many thanks. there are two points with the js ways,1,when you open the page. the first example one text is not bold. when you mouse out all the text, there is no one image showing.i want to show one, when the mouse out.
1

Check this:

$(document).ready(function(){
    $("div.first img").hide();
    $("ul li:first img").show();
    $("#top10 li").hover(function(){
        $(".first img").hide();
        $(this).find("img").show();
    });
});

Demo: http://jsfiddle.net/vxTRs/

3 Comments

thank U,now i want to make the example one text's font-weight to bold, when the mouse hove on the li. i add this line, $(this).children("div.last a").css("font-weight","bold");but there is no effect. ps: at first i set the font-weight:normal by css
@enjoylife you can add in your CSS .last a:hover {font-weight:bold}
thank you, but how to set the default state which is using bold to the first example one text. when the mouse hover on the image, the example one text will be bold and the others don't.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.