使用JavaScript来“链接”从HTML背景图像? [英] Using javascript to "link" from html background image?
问题描述
如果有人能指出我出错的地方,我将不胜感激。
我有以下html和javascript代码:
< html>
< head>
< link href =style1.css =stylesheettype =text / css>
< / head>
< div id =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;
背景颜色:黑色;
}
#body {
width:100%;
height:2000px;
background-image:url('uploads / 1.jpg');
光标:指针;
}
.container {
width:1000px;
margin-right:auto;
margin-left:auto;
height:1000px;
背景颜色:白色;
$ / code $ / pre
解决方案
.getElementById('container')
$ b 当container
也可以做到
< div class =containeronClick =goToWebsite()>< / div> ;
< script>
函数goToWebsite(){
window.location ='http://google.com';
}
< / script>
Having looked elsewhere on the website it seemed to me to be a bit tricky to get a background image in html to be a link also. In looking around I stumbled across somebody's suggestion that using java script should do the trick. However in testing the idea out i have had no luck.
I would be very grateful if somebody could point out where i am going wrong.
I have the following html and javascript code:
<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>
I also have the following 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; }
解决方案You have
.getElementById('container')
When
container
is actually a class.can also do
<div class="container" onClick="goToWebsite()"></div>
<script> function goToWebsite() { window.location = 'http://google.com'; } </script>
这篇关于使用JavaScript来“链接”从HTML背景图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!