2010-02-10 157 views
1
<img id="sun" href="logo.jpg"/> 

我该如何使用JQuery绑定它,以便当“sun”被点击时,会发生什么? 我只想绑定onclick到太阳。Jquery绑定事件

+0

什么是太阳在你的问题? – sberry 2010-02-10 00:05:13

+0

你的意思是这个太阳**☼**? – aefxx 2010-02-10 00:06:30

回答

2

如果太阳是一个id,

$('#sun').click(function(eo) { 
    // your code here 
}); 

如果太阳是一个类,

1

我假设“太阳”是ID为“太阳”的元素。

$("#sun").click(function(){ 
    alert("I was clicked!"); 
}); 
+0

$ {}应该是$() – 2010-02-10 00:13:00

+0

Lachlan - 你说得很对,谢谢。我已更新。 – 2010-02-10 00:28:48

0

如果太阳是一个id:

$("#sun").click(function(ev){ 
    // this refers to the dom element 
    alert("I was clicked!"); 
    return false 
}); 

也许是类:

$(".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的API参考: