Intercept Link Clicks from Frame
I have a old fashion page that use开发者_JAVA百科s a frameset containing a top frame and bottom frame. The frameset is defined in "index.html" and my code is as follows:
<html>
<head>
<script src="jquery-1.4.2.min.js" type="text/javascript"></script>
<script>
$(document).ready(function(){
$('#mainFrame').ready(function() {
$('#mainFrame a').live('click', function() {
alert('u click');
return false;
});
});
});
</script>
</head>
<frameset id="main" rows="125,*" cols="*">
<frame src="header.html" name="headerFrame" id="headerFrame" />
<frame src="main.html" name="mainFrame" id="mainFrame" />
</frameset>
</html>
I would like to be able to intercept links that are clicked on the frame "mainFrame". I thought I could just add a ready event for it then live bind click events but it doesn't work. Any ideas?
Note: The files are all on the same domain so this is not an XSS issue.
<script>
$(document).ready(function(){
$('#mainFrame').ready(function() {
var f = document.getElementById('mainFrame').contentWindow.document;
$('a', f).live('click', function() {
alert('u click');
return false;
});
});
});
</script>
You just need to add a context to jQuery statement, to tell it where to look for #mainFrame a
Oh yeah....you definitely need to make sure that the iframe is loaded. The "live" should take care of that...but I'm working on something now where I had to write a custom ready() plugin for jQuery to make sure my frames were loaded, because I was having trouble with Chrome.
Chrome and IE seem to jump the gun on with the document.addEventListener("DOMContentLoaded"... which I believe is what the ready() functions in jQuery use.
Anyways, just make sure that your iframes get loaded all the way.
精彩评论