jQuery为iframe的body添加click事件的实现代码


<html>
<head>
<title>为body添加click事件</title>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<script type="text/javascript" src="https://www.jb51.net/jquery-1.5.2.js"></script>
<script>
function show_body_click(){
alert('you are click the body');
}
jQuery(document).ready(function(){
//jQuery("ifr_id").contents().find("body").attr("onclick", "show_body_click()");
//alert(jQuery("#ifr_id").contents().find('body').attr("onclick",'xxxx()'));//不能实现
//var ifr = jQuery("ifr_id")
});
jQuery(document, '#ifr_id').ready(function(){
jQuery("#ifr_id").contents().find("body").attr("onclick", "parent.show_body_click()");
//jQuery("#ifr_id").contents().find("body").live("click", "parent.show_body_click()");//ff没有效果
//jQuery("#ifr_id").contents().find("body").click(parent.show_body_click()); //ff没有效果
});

</script>
</head>
<body>
<span>
<iframe src="https://www.jb51.net/eval.html"></iframe>
</span>
</body>
</html>

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wdfgxx.html