gpt4 book ai didi

javascript - HTML 通过 onload() 函数创建链接

转载 作者:行者123 更新时间:2023-11-28 06:22:59 25 4
gpt4 key购买 nike

我需要将 url 保存到 .txt 文件并使用某些 example.html 文件访问该 url。

下面是我从 .txt 文件获取 url 的代码。

<html>
<head>
<script>
function readfile() {
alert(document.getElementById('iframe').contentDocument.body.firstChild.innerHTML);
}
</script>
</head>
<body>
<iframe id='iframe' src = 'URL.txt' onload='readfile()' frameBorder='0' >&nbsp;</iframe>
</body>
</html>

我的 .txt 文件包含 https://www.google.co.in/

enter image description here

如何将网址包含在 anchor 标记中,使其看起来像下面的代码

<a href="" >Click Here to view</a>

最佳答案

将您的 readfile() 方法更改为

function readfile() 
{
var url = document.getElementById('iframe').contentDocument.body.firstChild.innerHTML;
document.body.innerHTML += "<a href='" + url + "'> Click Here to view </a>";
}

如果您的文本文件仅包含 URL,您可以尝试使用此方法

var url  = document.getElementById('iframe').contentDocument.body.innerHTML;

您还可以使用document.createElement创建元素

var anchor = document.createElement( "a" );
anchor.setAttribute( "href", url );
document.body.append( anchor );

关于javascript - HTML 通过 onload() 函数创建链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35355751/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com