我想获取当前URL地址,将其存储到一个变量中,然后传递给HTML元素:
var url = document.URL;
document.getElementById("url").innerHTML = url;
我尝试使用 document.URL
、window.location
和 window.location.href
,但它们都无法正常工作。它们什么也没有显示出来。
我的HTML代码如下:
<p id="url"></p>
提前感谢!
以下是我的源代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<style type="text/css">
input:hover
{
background: black;
color: white;
border: 0;
}
</style>
<script>
var url = location.href;
document.getElementById("url").innerHTML = url;
function first()
{
var string = "It works!";
write(string);
}
function write(szoveg)
{
alert(szoveg);
}
function submit()
{
var result;
result = confirm("Would you like to confirm your change?");
if(result==true)
window.location="okay.html";
else if(result==false)
alert("You have clicked the \"cancel\" button.");
}
</script>
</head>
<body>
<input type="button" onclick="first()" value="Try Me" />
<p onmouseover="submit()">Hover over this text.</p>
<p id="url">error</p>
</body>
</html>
location.href
没问题。你试过弹出它吗? - MaxArtdocument.getElementById("url")
为null
? - Bergi<p>
标签之后还是之前?如果在之前 - 当你执行JS时,该元素不存在。 - Zoltan Toth