如何使div可点击? [英] how to make div click-able?
本文介绍了如何使div可点击?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
< div>< span>上海< / span>< span>男< / span>< / div>
对于像上面的div,当鼠标打开时,它应该成为cursor:指针,
EDIT :和
如何在鼠标打开时更改div的背景颜色?编辑:如何使第一个span的宽度= 120像素?在firefox中工作
解决方案
给它一个像something的ID,然后:
var something = document.getElementById('something');
something.style.cursor ='pointer';
something.onclick = function(){
// do something ...
};
更改背景颜色(根据更新的问题):
something.onmouseover = function(){
this.style.backgroundColor ='red';
};
something.onmouseout = function(){
this.style.backgroundColor ='';
};
<div><span>shanghai</span><span>male</span></div>
For div like above,when mouse on,it should become cursor:pointer,and when clicked,fire a
javascript function,how to do that job?
EDIT: and how to change the background color of div when mouse is on?
EDIT AGAIN:how to make the first span's width=120px?Seems not working in firefox
解决方案
Give it an ID like "something", then:
var something = document.getElementById('something');
something.style.cursor = 'pointer';
something.onclick = function() {
// do something...
};
Changing the background color (as per your updated question):
something.onmouseover = function() {
this.style.backgroundColor = 'red';
};
something.onmouseout = function() {
this.style.backgroundColor = '';
};
这篇关于如何使div可点击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文