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.