attach event by click on a div
hi i have this html code :
<div id="#div"><a href="">Test</a></div>
i want attach click event on #div when click on div and not click in a tag,
开发者_如何学编程EDITED : Please view this page : http://jsfiddle.net/upVex/2/
i want change bg color when only click on space of #div
Took me a little while to figure out why it didn't work, but you can do that quite easily: http://jsfiddle.net/4FPYf/12/
And the code:
$(document).ready(function() {
  $('#div').click(function(e) {
    if ($(e.target).get(0) == $(this).get(0)) {
      alert('You clicked me');
    } else {
      alert('You clicked my child');
    }
  });
});
You need to attach a click hander for #div and also attach a click handler for the anchor element but stop the propagation of the vent to parent/s.
Try this:
$(function(){
    $("#div a").click(function(){
        e.stopPropagation();
    });
    $("#div").click(function(){
        //Your Code
    });
});
do you not mean this?
$('#div').click(function() {
 // Do stuff
});
or something like this?
$('#div').bind('click', function() {
// Do stuff
});
or even something like this?
$('#div>a').click(function() {
// Do Stuff
});
If you want to do something when the anchor is clicked i'd suggest the last one... but make the href = "#"
 
         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论