Home  >  Article  >  Web Front-end  >  jQuery: Analysis and introduction to the invalid problem of .click()

jQuery: Analysis and introduction to the invalid problem of .click()

黄舟
黄舟Original
2017-06-27 10:07:562027browse

This article mainly introduces the analysis of the invalid problem of $("a").click() in jQuery. Friends in need can refer to the

TodayformWhen the front desk exports to xls, I want to automatically trigger the click of a. But failed. Finally found this file.

I have tried many times to use jQuery to simulate the function of the user clicking on the a tag, but without success, and I have been troubled for a long time. I was in a daze some time ago, and a new idea came up, so I started testing it.

Look at the code below first:

<html>
<
head
>磨途歌-A标签测试1<head>
<body>
   <a href="http://blog.mo2g.com">磨途歌<a>
</body>
</html>
<script src="http://ajax.google
api
s.com/ajax/libs/jquery/1.7.2/jquery.min.js"><script>
<script>
jQuery(function($) {
  //给所有A标签绑定点击触发
事件
  $(&#39;a&#39;).click(function() {
    alert(1);
  });
  //触发所有A标签的点击事件
  $(&#39;a&#39;).click();
});
</script>

The code above has indeed triggered the event of clicking the A tag, but everyone must also have questions about why the A tag is not triggered when the A tag is clicked. Jump event?

At first I thought it was because the browser had taken corresponding security measures to block the operation of JS on the A tag. Later I found out that this was not the case. Let’s talk about it next. the whole story.

Before I start to explain, let me ask a question. When we click on the "A label", what exactly did we click on to cause the jump to occur?

1) Is the "A label" itself clicked?

2) Did you click on the text displayed in the "A label"?

Having said this, everyone should understand that our code above has confirmed that clicking on the A label itself will not trigger an event that jumps to the specified link. That is to say, we usually click on the A label. in the text?

Now that you have a clue, let’s give it a try.

 <html>
<head>磨途歌-A标签测试2<head>
<body>
   <a href="http://www.mo2g.com">磨途歌<a>
</body>
</html>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"><script>
<script>
jQuery(function($) {
  var mo2g = &#39;<span id="mo2g">磨延城<span>&#39;;
  //给A标签中的文字添加一个能被jQuery捕获的元素
  $(&#39;a&#39;).append(mo2g);
  //模拟点击A标签中的文字
  $(&#39;#mo2g&#39;).click();
});
</script>

The effect came out. Facts have proved that the above inference is correct. Therefore, if you want to use JS to simulate the click event of the A label, you must first add elements that can be captured by JS to the text in the A label. , and then use JS to simulate clicking on the element.

The above is the detailed content of jQuery: Analysis and introduction to the invalid problem of .click(). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn