<img id="sun" href="logo.jpg"/>我如何使用JQuery来绑定它,这样当"sun“被点击时,就会发生一些事情?我只想把onclick绑定到sun上。
发布于 2010-02-10 08:05:51
如果sun是一个id,
$('#sun').click( function(eo) {
// your code here
});如果sun是一个类,
$('.sun').click( function(eo) {
// your code here
}发布于 2010-02-10 08:05:15
我假设"sun“是一个id为"sun”的元素。
$("#sun").click(function(){
alert("I was clicked!");
});发布于 2010-02-10 08:11:52
如果Sun是一个id:
$("#sun").click(function(ev){
// this refers to the dom element
alert("I was clicked!");
return false
});也许是class:
$(".sun").click(function(ev){
// this refers to the dom element
alert("I was clicked!");
return false
});或者是一个类,元素可以在页面加载后很好地创建-例如通过AJAX或DOM操作。
$(".sun").live('click',function(ev){
// this refers to the dom element
alert("I was clicked!");
return false
});JQuery接口参考:
http://api.jquery.com/category/selectors/
https://stackoverflow.com/questions/2233462
复制相似问题