tags:

views:

91

answers:

5

HI,

I have this code for extracting the href and adding it to a parent block...

$(".new-dev").hover(function(){
    $(this).css('cursor','pointer');
    $('this').$('a').css('color','#696969');
},function(){
    $('this').$('a').css('color','#000');
});

What I want to do is make the a within the block change color on rollover. Unsure of how to do this when using $(this).

Any advice welcome.

Thanks, C

+2  A: 

Update: I thought one can do this with CSS but this approach would not work in all browser, e.g. not in IE6. If this is important for you, than you have to stick with the JS approach.
I also updated the code because I think I know understood what you really want:

Here you go (you got the syntax wrong):

$(".new-dev").hover(function(){
    $(this).css('cursor','pointer');
    $(this).find('a').css('color','#696969');
},function(){
    $(this).find('a').css('color','#000');
});

This applies the color changing to every a inside elements with class new-dev. But as you change the cursor style only once, a better solution might be:

$(".new-dev").css('cursor','pointer').hover(function(){
    $(this).find('a').css('color','#696969');
},function(){
    $(this).find('a').css('color','#000');
});

This applies the cursor style to all elements with class new_dev first and then applies the hover functions. There is no need to set the cursor style inside the hover function over and over again.

Read about selectors and traversing in the documentation.

Note: color changes the color of the font, if you want to change the background color, use background-color.

Felix Kling
@Felix hi Felix, you just wrote like seconds ago before me about the css :D I just tried the -f from PHP exec on CENTOS and it works, now I'm installing ubuntu to try it add a bit more things to make it not work and I show you the results.
c0mrade
@c0mrade: :-D But I forgot the pointer thing... you mean it worked to remove a folder without `-r`? Hui... At least it does not work on Debian and Mac OSX (so I think it won't work on Ubuntu as well).
Felix Kling
+1  A: 

If I think what you actually need is that you need to style anchor element to have some kind of change of css on hover event. You can do that with css

.new-dev a{
color:#000;
}

.new-dev:hover a{
color:#696969;
cursor:pointer;
}

If I however misunderstood your question, please update it with some HTML as well.

c0mrade
You missed the `a` tags buddy :)
Felix Kling
@Felix yes I did :D but you got correct answer anyways and +1
c0mrade
@Felix - We don't need no stinkin `a` tags =P
Nick Craver
+2  A: 

It looks like this is what you want, if you are looking to change every <a> in the block when hovering over the block, not each anchor individually.

Try this CSS, no need for javascript in this case:

.new-dev { cursor: pointer; }
.new-dev a { color: #000; }
.new-dev:hover a { color: #696969; }

cursor only affects on mouse over already, best to leave that in the CSS :)

However, if you wanted to animate between colors instead of just toggling, you could do this (still leave the cursor rule in the CSS):

$(".new-dev").hover(function(){
    $('a', this).stop().animate({ 'color' : '#696969' });
},function(){
    $('a', this).stop().animate({ 'color' : '#000000' });
});

This will fade smoothly between the colors, but you'll need the jQuery color plugin found here :)
Here's a quick demo of this approach

Nick Craver
Ah your are right with the `.new-dev:hover`. But afaik, some IE versions don't allow `:hover` on other elements than `a`, I just don't know whether it is only IE6 or also later versions.
Felix Kling
@Felix - Just IE6...but it's not a breaking thing, users with an ancient browser can live without some styling :)
Nick Craver
@Nick Craver: Yes I know it does not break, it just doesn't work :) (have I said how much I f**** hate IE6? ;))
Felix Kling
The animate doesn't work for some reason, but css does
Model Reject
@Felix - Amen, on my current project we were given the green light to drop IE6 support, they could grab chrome frame if they needed to access the app, that was a *goooood* day :)
Nick Craver
anyway, +1 for correct CSS (in contrast to what I wrote ;))
Felix Kling
@Clint - Woops, you'll need the color plugin for animation, forgot to mention: http://plugins.jquery.com/project/color I'll update that in case someone else comes across this
Nick Craver
A: 

You might wanna have a look at this jQuery plugin I wrote some time ago: clickable. If you call it on the block(s) you want to make clickable, like $(".new-dev").clickable(); then it extracts the href value of the first link within each block and use that to navigate to when the user clicks the block.

For styling, or additional scripting, it adds the following classNames as hooks: on the matching blocks:

  • .jsClickable
  • .jsClickableHover (onmouseover, as IE6 doesn't support :hover on elements other than links)
  • .jsClickableFocus (when guiding link gets focus)

on the link that provides the target location:

  • .jsGuide

So here the CSS would be:

.new-dev.jsClickable { cursor: pointer; }
.new-dev a { color: #000; }
.new-dev a:hover, .new-dev a:focus,
.jsClickableHover a.jsGuide, .jsClickableFocus a.jsGuide { color: #696969; }

This way the cursor only becomes a pointer in case JavaScript is enabled and the link also changes color if it gets focus, which makes it more accessible for keyboard navigation.

For downloading + documentation see the plugin's page: jQuery: clickable — jLix

Sander Aarts