无法使用click()函数单击锚标记

时间:2018-05-14 14:26:40

标签: javascript jquery

我想要而不是按anchor tag的链接来加载href attributre中的页面我希望anchor对用户不可见,并且可以通过提供的按钮进行点击。

这是代码:



<!DOCTYPE html>
<html>
<head>
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
	<script type="text/javascript">
		$(document).ready(function(){

    		$("button").click(function(){
        		$("a").click();
   		 	});

		});
	</script>
</head>
<body>
<body>
	<a href="http://www.touchwand.com/wp-content/uploads/2018/04/Icons-and-backgrounds.zip"></a>
	<button >Button1</button>
</body>
&#13;
&#13;
&#13;

如果我直接点击锚点我会到达URL,但是通过按钮它不起作用。为什么<a>功能未点击click()?为什么不起作用?

1 个答案:

答案 0 :(得分:-1)

你在哪个设备上进行测试? 在移动设备上,您必须获得初始用户交互才能执行Javascript。

您还可以执行以下操作:

$("button").on("click", function() {
    window.location.href = $("a")[0].href;
}

这会有帮助吗? 你能提供更多信息吗?

参考:How to get the browser to navigate to URL in JavaScript

相关问题