使用链接变量创建超链接
<html>
<body>
<center><h1> retrive data</h1></center>
<h1 id="head1"> </h1>
<input type="text" placeholder="enter your unique id" id="pass"/>
<input type = "button" value = "submit" id="but" onclick="myfunction();"/>
<script>
var pass;
function myfunction()
{
pass = document.getElementById("pass").value;
document.writeln(pass);
document.writeln("<br>");
document.writeln("<br>");
document.writeln("<br>");
document.writeln("<br>");
var passwordToLookFor = pass;
var ref = firebase.database().ref("users");
var query = ref.orderByChild("password").equalTo(passwordToLookFor);
query.once("value").then(function(snapshot) {
snapshot.forEach(function(child) { // loop over the results
console.log(child.key);
console.log(child.val().user_name);
var link = child.val().user_name;
document.writeln(link);
});
});
}
</script>
</body></html>
我想将链接的值创建为超链接 我希望在调用函数时一次创建超链接
答案 0 :(得分:2)
您只是在寻找如何使其成为定位标记的方法吗?
<script>
var pass;
function myfunction()
{
...
var link = child.val().user_name;
document.writeln("<a href='"+link+"' target='_blank'>"+link+"</a>");
});
});
}
</script>
</body></html>
答案 1 :(得分:0)
您可以像这样创建a
dom元素:
let link_el = document.createElement('a')
link_el.href = link // assuming link holds the value of the href you want
然后将其插入到所需的dom中。
答案 2 :(得分:0)
如果我理解正确,并且link
变量包含您要导航到的实际地址,那么它将起作用。首先,只需在要填充链接的div上设置一个ID:
<div id="target-div"></div>
然后像这样填充它(我只是出于演示目的创建了一个数组,但这将是您的快照。forEach:
var links = ['link1', 'link2', 'link3']
var targetDiv = document.getElementById("target-div");
links.forEach(function(link) {
var anchor = document.createElement('a');
anchor.href = link;
anchor.innerText = link;
targetDiv.appendChild(anchor);
var br = document.createElement('br');
targetDiv.appendChild(br);
});