Invalid $. click () Problem Analysis in jQuery, jquery. click

Source: Internet
Author: User

Invalid $. click () Problem Analysis in jQuery, jquery. click

When exporting a table to xls at the front end today, You want to automatically trigger a click. But failed. Finally, find the file.

I tried to use jQuery multiple times to simulate the user clicking the tag, but it was not successful, and it had been plagued for a long time. Some time ago, a daze came up with a new idea, so I started the test.

Let's take a look at the following code:

Copy codeThe Code is as follows:
<Html>
<Head> motiange-A tag Test 1 <Body>
<A href = "http://blog.mo2g.com"> motige <a>
</Body>
</Html>
<Script src = "http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"> <script>
<Script>
JQuery (function ($ ){
// Bind A click trigger event to all A tags
$ ('A'). click (function (){
Alert (1 );
});
// Trigger all click events of A tag
$ ('A'). click ();
});
</Script>

The above code has indeed triggered the click A tag event, but you may also have doubts. Why did you click the tag but do not trigger the jump event of the tag?

At first, I thought it was the browser that made corresponding security measures to block JS operations on the tag. Later I found that this was not the case. Next I will talk about the original principles.

I will throw a question before I start to explain it. When we click "A tag", what does the jump happen after we click it?

1) is the "A tag" itself clicked?

2) is the text displayed in "A tag" clicked?

Here, we should understand that the code above has confirmed that clicking the tag itself does not trigger events that jump to the specified link, that is, we usually click the text in label?

If you have a clue, try it.

Copy codeThe Code is as follows:
<Html>
<Head> motiange-A tag Test 2 <Body>
<A href = "http://www.mo2g.com"> motige <a>
</Body>
</Html>
<Script src = "http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"> <script>
<Script>
JQuery (function ($ ){
Var mo2g = '<span id = "mo2g"> moyancheng <span> ';
// Add an element that can be captured by jQuery to the text in tag
$ ('A'). append (mo2g );
// Simulate clicking the text in tag
$ ('# Mo2g'). click ();
});
</Script>

This is the result. Facts have proved that the above inference is correct. Therefore, to simulate clicking A tag event in JS, you must add elements that can be captured by JS to the text in the tag, then, use JS to simulate and click this element.

The above is an analysis of the invalid $ ("a"). click () Problem in jQuery mentioned in this article. I hope my friends will like it.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.