omg*_*omg 28 html javascript css click
<div><span>shanghai</span><span>male</span></div>
Run Code Online (Sandbox Code Playgroud)
对于像上面这样的div,当鼠标开启时,它应该变成光标:指针,当点击时,触发一个
javascript函数,该怎么做?
编辑:以及如何在鼠标开启时更改div的背景颜色?
编辑再次:如何使第一个跨度的宽度= 120px?似乎不能在Firefox中工作
Jam*_*mes 50
给它一个像"某事"的ID,然后:
var something = document.getElementById('something');
something.style.cursor = 'pointer';
something.onclick = function() {
// do something...
};
Run Code Online (Sandbox Code Playgroud)
更改背景颜色(根据您更新的问题):
something.onmouseover = function() {
this.style.backgroundColor = 'red';
};
something.onmouseout = function() {
this.style.backgroundColor = '';
};
Run Code Online (Sandbox Code Playgroud)
我建议使用jQuery:
$('#mydiv')
.css('cursor', 'pointer')
.click(
function(){
alert('Click event is fired');
}
)
.hover(
function(){
$(this).css('background', '#ff00ff');
},
function(){
$(this).css('background', '');
}
);
Run Code Online (Sandbox Code Playgroud)
其中最简单的:
<div onclick="location.href='where.you.want.to.go'" style="cursor:pointer"></div>
Run Code Online (Sandbox Code Playgroud)
我建议使用一个名为clickbox的CSS类,并使用jQuery激活它:
$(".clickbox").click(function(){
window.location=$(this).find("a").attr("href");
return false;
});
Run Code Online (Sandbox Code Playgroud)
现在,您唯一要做的就是将div标记为可点击并提供链接:
<div id="logo" class="clickbox"><a href="index.php"></a></div>
Run Code Online (Sandbox Code Playgroud)
加上CSS样式来改变鼠标光标:
.clickbox {
cursor: pointer;
}
Run Code Online (Sandbox Code Playgroud)
容易,不是吗?
| 归档时间: |
|
| 查看次数: |
187162 次 |
| 最近记录: |