在网站的其他地方看过之后,在我看来,将 html 中的背景图片也作为链接有点棘手。在环顾四周时,我偶然发现了某人的建议,即使用 java 脚本应该可以解决问题。然而,在测试这个想法时,我没有运气。
如果有人能指出我哪里出错了,我将不胜感激。
我有以下 html 和 javascript 代码:
<html>
<head>
<link href = "style1.css" rel = "stylesheet" type = "text/css">
</head>
<div id = "header">
Header
</div>
<body>
<div id = "body">
<div class = "container">
</div>
</div>
</body>
</html>
<script>
document.getElementById('container').onclick = function() {
window.location = 'http://www.google.com/';
}
</script>
我还有以下 CSS:
#header{
width:100%;
height:50px;
background-color:black;
}
#body {
width:100%;
height:2000px;
background-image:url('uploads/1.jpg');
cursor:pointer;
}
.container{
width: 1000px;
margin-right: auto;
margin-left: auto;
height: 1000px;
background-color:white;
}
最佳答案
你有 .getElementById('container')
当 container
实际上是一个类。
也可以做到<div class="container" onClick="goToWebsite()"></div>
<script>
function goToWebsite() {
window.location = 'http://google.com';
}
</script>
关于javascript - 使用 javascript 从 html 背景图像到 "link"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14697735/