您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

如何在 JavaScript 中通过超链接定位特定框架?

2024/4/20 16:14:08发布23次查看
html 框架提供了一种将浏览器窗口划分为多个部分的便捷方法。
其中每个部分都可以加载单独的 html 文档。我们可以使用 javascript 将内容加载到特定的框架中,使用窗口对象的框架属性。 frames 属性是一个类似数组的对象,包含当前页面上的所有框架(包括 iframe)。
我们可以通过多种方式使用 window.frames[] 属性将文档的内容加载到框架中。让我们一一看看它们 -
1。使用索引要定位特定框架,您可以使用框架的索引或名称。例如,要定位页面上的第一帧,您可以使用以下代码 -
window.frames[0].document.location.href = http://www.example.com
2。使用框架名称要使用其名称来定位框架,可以使用以下方法。假设框架的名称是“frame_name” -
window.frames[frame_name].document.location.href = http://www.example.com;
3。使用 getelementbyid 和 getelementbyname您还可以使用 getelementbyid() 或 getelementsbyname() 方法定位框架,然后使用方法 contentwindow 访问框架窗口,如下所示 -
document.getelementbyid(frame_id).contentwindow.location.href = http://www.example.com;document.getelementsbyname(frame_name)[0].contentwindow.location.href = http://www.example.com;
示例以下是包含所有这些方法的完整工作代码片段 -
<!doctype html><html><head> <title>target a frame</title></head><body> <button onclick=indexmethod()>using index</button> <button onclick=namemethod()>using frame name</button> <button onclick=querymethod()>using query methods</button> <iframe src= height=150px width=100% name=frame_name id=frame_id srcdoc=<html> <body style='background-color:#ccc;'> <h1>testing iframe</h1> </body> </html>> </iframe> </body> <script> const indexmethod = () => { const child = document.createelement('p'); child.innertext = 'added inside frame'; window.frames[0].document.body.appendchild(child); }; const namemethod = () => { const child = document.createelement('p'); child.innertext = 'added inside frame'; window.frames[frame_name].document.body.appendchild(child); }; const querymethod = () => { const child = document.createelement('p'); child.innertext = 'added inside frame'; document.getelementbyid(frame_id).contentwindow.document.body.appendchild(child); }; </script></html>
以上就是如何在 javascript 中通过超链接定位特定框架?的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product