Reputation: 20667
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.
Upvotes: 0
Views: 464
Reputation: 20667
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.
Upvotes: 0
Reputation: 19358
An interesting approach may be to create new rules using a plugin like jQuery.Rules.
Upvotes: 1
Reputation: 2859
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.
Upvotes: -1
Reputation: 7266
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();
Upvotes: 0
Reputation: 9055
$('#links td.active a').hover(
function()
{
$(this).data('prevColor', $(this).css('color')).css('color', '#FF0000');
},
function()
{
$(this).css('color', $(this).data('prevColor'));
});
Upvotes: 6