0

I have a page that load contents using

$('#header').load('head.html'), but the links inside head.html cannot use my website.css because website.css loaded earlier or for another reason.

so I thought this $('a:hover').css('color','green'); but no success.

I can solve with tags but I dont want to do it.

Update:


base.html
<script> 
    $('#header').load('/restaurant{{ chosenRest }}.html'); //chosenRest=1,2..etc
</script>

<div id="accordion">
<div id="header" >   </div></div>

I have files like, restaurant1.html, restaurant2.html


restaurant1.html
<a href="/Giorgio"> Giorgio's </a>   

website.css

a:hover {color:green};


now if I add something like <style> a:hover {color:green; } </style> to base.html it works, but I don't want to do that way.

update: Finally Solved

here what worked:

base.css I added a new class

.restaurant:a:hover {
      color:green;
} 

restaurant1.html

<a href="/Giorgio" class="restaurant"> Giorgio's </a>  
Emmet B
  • 5,341
  • 6
  • 34
  • 47
  • 3
    css has no issue with time of loading so it should use website.css and however by jquery `$('a').hover(function(){$(this).css('color','green');},function(){$(this).css('color','prev_one');});` – bugwheels94 Apr 16 '13 at 07:43
  • possible duplicate of [How do you add pseudo classes to elements using jQuery?](http://stackoverflow.com/questions/12740967/how-do-you-add-pseudo-classes-to-elements-using-jquery) – jbl Apr 16 '13 at 07:48
  • Check your `CSS`. probably you have something like `#body a:hover{}` then you load your a in `#header`. Post `CSS` here for more info. – Narek Apr 16 '13 at 07:49
  • CSS will be applied even to elements added after the page is loaded. There is something else going on, does your head.html contain an iframe or something funky like that? Can you post the contents of the head.html ? – xec Apr 16 '13 at 07:50
  • @narek I checked I only have `a:hover {color:green;}` – Emmet B Apr 16 '13 at 07:50
  • 1
    @jbl The jquery here is confusing, but not really essential to the question. Not a duplicate (at least not of what you linked) – xec Apr 16 '13 at 07:53
  • http://stackoverflow.com/questions/2053693/override-ahover-with-jquery is very relevant I think, I will implement that solution if I cant find any better. – Emmet B Apr 16 '13 at 07:57
  • 2
    http://jsfiddle.net/narekmarkosyan/F8MYx/1/ as you can see here link hover works on delayed loaded html. Problem in your `HTML/CSS` structure. If you post it here answer will be found fast and **witout** jQuery. – Narek Apr 16 '13 at 08:01
  • thanks @Narek, it makes clear sense, I think there may be difference between .load vs .html, I am preparing a jsfiddle now. – Emmet B Apr 16 '13 at 08:04
  • 1
    Problem is what you cant add load in jsfiddle, because loaded file need to be on the same domain, but as I know load get data from addres and make `$().html()` so in jquery level it's same. – Narek Apr 16 '13 at 08:19
  • @Narek both points are right, when I tried to add your .html() code, it did not work either. So I think there is a more general problem, I ll paste in a bit all the structure. – Emmet B Apr 16 '13 at 08:20

2 Answers2

2

Use .on() with the mouseenter and mouseleave events and delegate the events:

$(document.body).on('mouseenter','a',function()
 {
  $(this).data("oldcolor",$(this).css('color')); //Store old color
  $(this).css('color','green');
 }).on('mouseleave','a',function()
 {
  $(this).css('color',$(this).data('oldcolor')); //retrieve old color
 }
)

Use data() to store the old color


Alternatively, you can do this with vanilla JS:

 document.styleSheets[0].insertRule('a:hover { color: green; }', 0);

Or, just add a:hover { color: green; } to a stylesheet, but you may not want to do that (this will be there from the start, whereas via JS you can choose when to add the rule)

Manishearth
  • 14,882
  • 8
  • 59
  • 76
0

Probably CSS doesn't load when you make load. just add DOM ready for your load.

<script> 
$(function(){
    $('#header').load('/restaurant{{ chosenRest }}.html'); //chosenRest=1,2..etc
});
</script>
Narek
  • 3,813
  • 4
  • 42
  • 58