views:

202

answers:

5

Hello all,

I am creating a customize site page that dynamically changes the current page so that you can see a preview of what you are changing. Everything is working pretty well, except that the code I'm using apparently can't handle pseudo-classes such as :hover and :visited.

The code is very simple, I am basically doing the following:

$("#links td.active a:hover").css("color", "#ff0000");

However, this doesn't actually set the <a> tag's hover color to #ff0000. It works fine if I take off the :hover though. Anybody have an suggestions as to how to get this to work?

Thanks very much!

Edit 1: Apparently, I might be going about it wrong altogether. Some more information shows that I might be able to use document.styleSheets.inlinestyle.rules to modify it, although this apparently only works in IE. Any more ideas would be greatly appreciated.

+6  A: 
$('#links td.active a').hover(
  function()
  {
    $(this).data('prevColor', $(this).css('color')).css('color', '#FF0000');
  },
  function()
  {
    $(this).css('color', $(this).data('prevColor'));
  });
Lior Cohen
A: 

The reason this doesn't work is that $("#links td.active a:hover") matches elements with the psuedo-class "hover", which at that time will be none. You'll need to add an event to do this, like Lior's answer.

$("#links td.active a").hover();

jthompson
A: 

You may put the CSS in a different class, and add it to the elements when you wish.

#links td.active a:hover { color: #yourdefaultcolor; }
#links td.active a.preview:hover { color: #ff0000; }

Use something like this in JavaScript:

$('#links td.active a').hover(
  function(){ $(this).toggleClass("preview", true);  },
  function(){ $(this).toggleClass("preview", false); }
);

It works better if you have more than a single attribute that needs changing.

danielkza
+1  A: 

An interesting approach may be to create new rules using a plugin like jQuery.Rules.

Joel Potter
Yeah, I just found that, the demo page doesn't appear to do what I need. It works for everything except pseudo-classes. I also tried http://plugins.jquery.com/project/jquerycssrule which should work, but simply doesn't.
Topher Fangio
jQuery.CssRule seems to be the answer. How are you trying to use it?
BYK
@BYK - `jQuery.cssRule(selector, attribute, color);` Didn't seem to work for any selectors. I know the variables (selector, attribute, color) are correct because I printed them out.
Topher Fangio
Be careful with the usage. You need to do jQuery.cssRule({'selector': [['attribute1', 'attributeValue1'], ['attribute2', 'attributeValue2']]});
BYK
@BYK - I modified it to use `$.tocssRule("body { color: white }");` and it worked except for the hover. I'm wondering if a combination of this and livequery might work...
Topher Fangio
Sadly, the following works almost perfectly. The only problem is that if you set the `:visited` after the `:hover` the preview doesn't show that, but other than that, it works:`$('head').append('<style>' + selector + ' { ' + attribute + ': ' + color + ' }</style>');`
Topher Fangio
A: 

Well, I finally figured out how to make it work like I wanted. Here is the basic code:

function updatePageCSS(input, color) {
  $('style.new_css_styles').remove();

  var new_css_styles = "<style class='new_css_styles'>";

  $('input.colorPicker').each(function() {
    var id = $(this).attr('id');
    var selector = $('#' + id + '_selector').val();
    var attribute = $('#' + id + '_attribute').val();
    var new_color = color;

    if ($(this).attr('id') != $(input).attr('id')) {
      new_color = $(this).val();
    }

    new_css_string += selector + ' { ' + attribute + ': ' + new_color + ' }\n';
  });

  new_css_styles += "</style>";

  $('head').append(new_css_styles);
}

Note that the selector and attribute are the values of hidden inputs. While I don't really like this approach very greatly, it seems to get the job done.

Topher Fangio