FINAL EDIT:
There are two problems:
Use $('#source').contents()
instead of simply using $('#source')
The css rule for .hover
is not visible in the context of the iframe.
Either use .css()
to set style directly, add the css links in demo.php
or clone all styles in the main document into the iframe with jQuery.
Also you should avoid .live
as there seem to be some issues inside iframes (when using live jQuery binds special eventHandlers on the document, and checks events when they bubble up)
Here is a working example (jsFiddle link):
var $c = $('#source').contents();
//clone all styles from this document into the iframe
$c.find('head').append($('style, link[rel=stylesheet]').clone());
$c.find('body').prepend('<b>This is a test</b><br><b>Click here</b>');
$c.delegate('b', 'hover', function() {
$(this).toggleClass('hover');
});
$c.delegate('b', 'click', function() {
alert('You clicked on:' + $(this).text());
});
ORIGINAL ANSWER:
Your problem is that you are using the iframe as context. You should use the frame document instead.
Also just for safety you might want to add your code in the onload event like this:
var doc = window.frames['source'].document;
$(doc).ready(function(){
$('body').prepend('This is outside the iframe<br>');
$('body',doc).prepend('This is inside the iframe<br>');
});
You can view this live jsFiddle example.
EDIT 1:
Ok, so the actual problem is that the css styles are not visible within the iframe.
For example if you use $(this).css('color':'red')
instead of addClass
it would work. See the updated jsFiddle
What I suggest is either to have the correct css styles already in demo.php
or inserted afterwards like so:
$('head',doc).append($('style, link[rel=stylesheet]').clone());
updated jsFiddle - with addClass and cloning of styles