iframejs,iframejs图片放大旋转

http://www.itjxue.com  2023-01-22 21:32  来源:未知  点击次数: 

如何向iframe中写入js代码?让js在iframe中执行

1、首先,新建一个网页

2、然后,为button按钮添加onclick事件,响应函数名的addiframeclick()。

3、再添加js脚本框架,并写出addiframeclick()函数的声明。

4、addiframeclick()函数的作用是为iframe框架添加onclick()事件。

5、这样,当点击button按钮后,会给iframe框架添加onclick()事件,点onclick()事件的响应效果是,点击iframe框架弹出信息提示。

js文件中iframe是什么意思

iframe是嵌入式框架,一般用来在已有的页面中嵌入另一个页面。

需要注意的是,iframe在html5中,已经有限支持了(只有src属性)。

js中监听iframe点击事件

在js中,需要监听iframe的点击事件,但是因为浏览器的同源策略,是无法监听到的,只能另辟蹊径去解决它

注意,仅限于pc网站,意思就是必须要有鼠标移入移出iframe的操作

!DOCTYPE?HTML?PUBLIC?"-//W3C//DTD?HTML?4.01?Transitional//EN"

html

head

meta?http-equiv="Content-Type"?content="text/html;?charset=ISO-8859-1"

titleDetect?IFrame?Clicks/title

script?src="./jquery-3.4.1.min.js"/script

script?type="text/javascript"

????$(document).ready(function()?{

????????var?currentObj=Object

????????var?isOverIFrame?=?false;

????????function?processMouseOut(e)?{

????????????console.log('out?iframe',e.target);

????????????currentObj=e.target

????????????isOverIFrame?=?false;

????????????top.focus();

????????}

????????function?processMouseOver(e)?{

????????????//?log("IFrame?mouse??OVER??detected.");

????????????console.log('in?iframe',e.target);

????????????currentObj=e.target

????????????isOverIFrame?=?true;

????????}

????????function?processIFrameClick(event)?{

????????????if(isOverIFrame)?{

????????????????console.log('click?iframe',event.target,currentObj);

????????????}

????????}

????????function?attachOnloadEvent(func,?obj)?{

????????????if(typeof?window.addEventListener?!=?'undefined')?{

????????????????window.addEventListener('load',?func,?false);

????????????}?else?if?(typeof?document.addEventListener?!=?'undefined')?{

????????????????document.addEventListener('load',?func,?false);

????????????}?else?if?(typeof?window.attachEvent?!=?'undefined')?{

????????????????window.attachEvent('onload',?func);

????????????}?else?{

????????????????if?(typeof?window.onload?==?'function')?{

????????????????????var?oldonload?=?onload;

????????????????????window.onload?=?function()?{

????????????????????????oldonload();

????????????????????????func();

????????????????????};

????????????????}?else?{

????????????????????window.onload?=?func;

????????????????}

????????????}

????????}

????????function?init()?{

????????????var?element?=?document.getElementsByTagName("iframe");

????????????for?(var?i=0;?ielement.length;?i++)?{

????????????????element[i].onmouseover?=?processMouseOver;

????????????????element[i].onmouseout?=?processMouseOut;

????????????}

????????????if?(typeof?window.attachEvent?!=?'undefined')?{

????????????????top.attachEvent('onblur',?processIFrameClick);

????????????}

????????????else?if?(typeof?window.addEventListener?!=?'undefined')?{

????????????????top.addEventListener('blur',?processIFrameClick,?false);

????????????}

????????}

????????attachOnloadEvent(init);

????});

/script

/head

body

iframe?src=""?width="80%"?height="600px"/iframe

iframe?src=""?width="80%"?height="600px"/iframe

/form

/body

/html

复制上边整段html,打开console控制台,即可解决这个问题,亲测好用

(责任编辑:IT教学网)

更多

推荐软件水平考试文章