开发者

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

开发者 https://www.devze.com 2023-04-13 08:34 出处:网络
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.

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

开发者_JAVA百科
<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.

0

精彩评论

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