使用JavaScript来“链接”从HTML背景图像? [英] Using javascript to "link" from html background image?

查看:78
本文介绍了使用JavaScript来“链接”从HTML背景图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在网站的其他地方看起来,在html中获得背景图像也是一个棘手的问题。在环顾四周,我偶然发现有人建议使用java脚本应该做到这一点。但是在测试这个想法的时候,我没有幸运。



如果有人能指出我出错的地方,我将不胜感激。



我有以下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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆