<iframe src="/demo.php" id="source"></iframe>
$('#source').delegate('*', 'hover', function() {
$(this).addClass('hover');
});
$('#source').delegate('*', 'mouseout', function() {
$(this).removeClass('hover');
});
$('#source').delegate('*', 'click', function() {
alert($(this).html());
return false;
});
Nothing happens when I mouseover or click on any element inside of the Iframe. The Iframe is on the same domain and I was/am under the impression that as long as the Iframe src is on the same domain it should work.
Any ideas on how to make this work?
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 indemo.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):
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:
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 ofaddClass
it would work. See the updated jsFiddleWhat I suggest is either to have the correct css styles already in
demo.php
or inserted afterwards like so:updated jsFiddle - with addClass and cloning of styles
try with this contents:
demo.php:
code: