开发者

Jquery binding event

开发者 https://www.devze.com 2022-12-19 13:16 出处:网络
<img id=\"sun\" href=\"logo.jpg\"/> How do I use JQuery to bind it so that when \"sun\" gets clicked, something happ开发者_如何学Pythonens?
<img id="sun" href="logo.jpg"/>

How do I use JQuery to bind it so that when "sun" gets clicked, something happ开发者_如何学Pythonens? I just want bind onclick to sun.


If sun is an id,

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

if sun is a class,

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


I'm assuming "sun" is an element with the id "sun".

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


If Sun is an id:

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

Perhaps it is class:

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

Or maybe a class, and the element may be created well after page load - eg via AJAX or DOM manipulation.

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

JQuery API references:

  • selectors http://api.jquery.com/category/selectors/
  • .click() http://api.jquery.com/click/
  • .live() http://api.jquery.com/live/
0

精彩评论

暂无评论...
验证码 换一张
取 消