这是我的代码片段:
function myFunction() {
var x = document.getElementById("myTextarea").value;
document.getElementById("demo").innerHTML = x;
}
<!DOCTYPE html>
<html>
<body>
<textarea id="myTextarea"></textarea>
<button type="button" onclick="myFunction()">Transform into link</button>
<a href="" target="_blank" id="demo"></a>
<script src="links.js"></script>
</body>
</html>
不知何故,当我点击创建的链接时,它没有打开。有点卡在这一点上。
最佳答案
你做得对,你只需要使用href而不是innerHTML。
function myFunction() {
var x = document.getElementById("myTextarea").value;
document.getElementById("demo").href = x;
}
<!DOCTYPE html>
<html>
<body>
<textarea id="myTextarea"></textarea>
<button type="button" onclick="myFunction()">Transform into link</button>
<a href="" target="_blank" id="demo">My Link!</a>
<script src="links.js"></script>
</body>
</html>
关于javascript - 如何将文本区域转换为链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52948877/