简体   繁体   中英

click( ) or trigger( 'click' ) does not work

I am using the jquery live method for an anchor element and if I click the anchor then click delegation works as expected. But if I try this on debug console by triggering click event, it never works. Please check the below example

<a href="#" id="test">click me</a>

$('#test').live('click',function(){
      alert('live click event');
});

If I try to trigger it on console like below. It never works.

$('#test').trigger('click');

Does anyone know what's causing this problem?

Note: There are no errors in the Javascript console.

I think your code is fine, so one of the following might be the cause of the problem:

  1. As mentioned in a comment before, you might have another tag with the same id (I know this is not the case, but if other people will read this, we might be able to help them by enlisting all imaginable causes of the problem)

  2. You might have an event on your page which deletes the tag, so when you try to query it in your console the tag doesn't exist any more, in this case you try to get a nonexistent object

  3. The given tag might be created only on a certain event and maybe when you try to reach the tag it doesn't exist yet, because the event which triggers it was not triggered yet

  4. You might have a naming issue

If none of the above matches your case, you should try to run any other jquery command. If other jquery commands fail too then the source of the problem might not be where you are looking for it.

If none of the ideas above help you, you might consider giving us more details about the structure (markup) and functionality of your system. Maybe someone is able to help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM