如何使用用户脚本将框架添加到现有框架集?

编程入门 行业动态 更新时间:2024-10-24 15:18:51
本文介绍了如何使用用户脚本将框架添加到现有框架集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

目标页面具有简单的HTML设置:

The target page has a simple HTML setup:

<html> <body> <frameset id="mainset" rows="50%,50%"> ... </frameset> </body> </html>

,我想使用JavaScript添加框架.

and I'd like to add frames using JavaScript.

这不起作用:

var parent = document.getElementsByTagName("frameset")[0]; var child = document.createElement("frame"); child.src = "about:blank"; child.style = "background-color: red;"; parent.appendChild(child);

这确实有效,但是会创建一个iframe:

This does work, but it creates an iframe:

var parent = document.getElementsByTagName("body")[0]; var child = document.createElement("iframe"); child.src = "about:blank"; child.style = "background-color: red;"; parent.appendChild(child);

是否可以使用javascript将框架添加到框架集中?

Is there a way to add frames to the frameset using javascript?

对不起,如果您之前曾问过这个问题.我搜索并找到了很多解决类似问题的人的解决方案",但是这些似乎不适用于此处.

Sorry if this question has been asked before. I searched and found a lot of 'solutions' for people having similar problems, but those don't seem to apply here.

目标页面如下:

推荐答案

仅添加新的<frame>是不够的.您还必须调整包含的<frameset>的rows或cols属性.

It's not enough to add a new <frame>. You must also adjust the rows or cols attribute of the containing <frameset>.

此外,如果框架集具有id,则 使用 ,而不是尝试通过标记名获取节点. (ID快速且唯一.)

Also, if the frameset has an id, use it instead of trying to get the node by tagname. (Ids are fast and unique.)

这样的代码可以使用(暂时):

Code like this will work (for now):

您可以 在此jsBin上查看运行中的代码 .

You can see the code in action at this jsBin.

var parent = document.getElementById ("mainset"); var child = document.createElement ("frame"); child.style = "background-color: pink;"; parent.appendChild (child); parent.rows = "30%,30%,30%" var docRdyInt = null; var frmCW = child.contentWindow; if (frmCW) { //-- Must wait for the new frame to be writable, esp in Firefox. docRdyInt = setInterval (createFramedPage, 50); } else { alert ("Oopsie! may be a rendering delay in some cases. Try code from console."); } function createFramedPage () { if (frmCW.document.readyState == "complete") { clearInterval (docRdyInt); frmCW.document.body.innerHTML = '<p>Hello World!</p>'; } }

更多推荐

如何使用用户脚本将框架添加到现有框架集?

本文发布于:2023-11-27 16:02:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1638668.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:框架   如何使用   脚本   用户

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!