【发布时间】:2010-02-10 08:28:31
【问题描述】:
<img id="sun" href="logo.jpg"/>
如何使用 JQuery 来绑定它,以便在单击“sun”时发生某些事情? 我只想将 onclick 绑定到 sun。
【问题讨论】:
-
您的问题中的太阳是什么?
标签: javascript jquery jquery-selectors
<img id="sun" href="logo.jpg"/>
如何使用 JQuery 来绑定它,以便在单击“sun”时发生某些事情? 我只想将 onclick 绑定到 sun。
【问题讨论】:
标签: javascript jquery jquery-selectors
如果 sun 是一个 id,
$('#sun').click( function(eo) {
// your code here
});
如果 sun 是一个类,
$('.sun').click( function(eo) {
// your code here
}
【讨论】:
我假设“sun”是一个 id 为“sun”的元素。
$("#sun").click(function(){
alert("I was clicked!");
});
【讨论】:
如果 Sun 是一个 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 参考:
【讨论】: