<a>标签无法触发jquery trigger('click')事件的解决

柠檬 前端 2019-08-16

先看如下代码:

<script src="https://img.cdn.apipost.cn/mp/3d9e9643852e31a1.webp"></script>
<script type="text/javascript">
$(function(){
	$(".click").trigger("click");
});
</script>
<a  target="_blank" href="https://url.apipost.cn/url?https%3A%2F%2Fwww.xxx.com%2F"  rel="external nofollow"  class="click">跳转中...</a>

这段代码执行后,也没并没有跳转,原因是<a>标签无法通过trigger触发click

解决,在<a>里嵌套一个<span>,触发该<span>的trigger即可。

<script src="https://img.cdn.apipost.cn/mp/3d9e9643852e31a1.webp"></script>
<script type="text/javascript">
$(function(){
	$(".click span").trigger("click");
});
</script>
<a  target="_blank" href="https://url.apipost.cn/url?https%3A%2F%2Fwww.xxx.com%2F"  rel="external nofollow"  class="click"><span>跳转中...</span></a>

评论