admin管理员组

文章数量:1400469

I have div with a few links spread out in the content. I would like to highlight all the links in the div onmouseover. Is there jquery solution that works in FF, IE and chrome.

Thanks.

I have div with a few links spread out in the content. I would like to highlight all the links in the div onmouseover. Is there jquery solution that works in FF, IE and chrome.

Thanks.

Share Improve this question asked Sep 24, 2010 at 16:53 NickNick 3854 gold badges8 silver badges16 bronze badges
Add a ment  | 

6 Answers 6

Reset to default 4

How about no JavaScript?

Style.CSS

.linkdiv a {
   color: blue;
}

.linkdiv:hover a {
   color: red;
}

I wanted to test this, but sadly jsfiddle isn't iPhone patible :(

Demo

HTML:

<div id='links'>
    This is simple text<br />
    <a href='#'>Link1<a/><br />
    <a href='#'>Link2<a/><br />
    <a href='#'>Link3<a/><br />
</div>

jQuery:

$('#links').live('mouseover', function(){
    $('#links > a').addClass('highlight');
});

$('#links').live('mouseout', function(){
    $('#links > a').removeClass('highlight');
});

CSS:

.highlight {
 background-color : red;   
}​



You can edit CSS part to highlight in your favorite style.

The Best Solution, As far as my concern =)

Markup :

<h1>CSS is cool! </h1>
<ul id="css">
    <li><a class="links" href="#"> Link1 </a></li>  
    <li><a class="links" href="#"> Link2 </a></li>  
    <li><a class="links" href="#"> Link3 </a></li>  
    <li><a class="links" href="#"> Link4 </a></li>
</ul>

CSS :

 #css li { margin:0px 5px;list-style:none; float:left;}
 #css .links { color :#0099f9; text-decoration:none;font:bold 20px Arial;}
 #css:hover a.links { color : #f0f;}

something like this (in your document ready) should do it!

$('#MyDiv').live('mouseenter', function(){
    $(this).find('a').addClass('highlight');
});
$('#MyDiv').live('mouseleave', function(){
    $(this).find('a').removeClass('highlight');
});

Give all the links the same class and then do this:

$(document).ready(function() {
  $('.someClass').hover(function() {
    $('.someClass').css('underline' : 'solid 1px #FFF');
  });
})

If I remember correctly, you should be able to just do:

$('div selector').hover(function(e) {
    $(this).find('a').doThings();
},
function(e) {
    $(this).find('a').undoThings();
});

I'd also suggest switching the explicit $.hover() call (just provided as an example) to use $.delegate() or $.live().

本文标签: javascriptHow to highlight links in the div on mouseoverStack Overflow