这是我的代码的一个人为的例子:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1"/>
<title></title>
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon"/>
<link rel="stylesheet" type="text/css" href=".css"/>
</head>
<body>
<div>
<a href="http://www.google.com" class="foo">YahOO</a>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
$("document").ready(function() {
$("#foo").trigger('click');
});
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
我想在页面加载时立即触发链接.以上不起作用,是不正确的?怎么可以这样做?
Ror*_*san 16
您foo在元素上设置了类,而不是id您需要使用的类.foo.另外,正如@JusticeErolin指出的那样,文档就绪处理程序不需要引号.试试这个:
$(document).ready(function() {
$(".foo").trigger('click');
});
Run Code Online (Sandbox Code Playgroud)
作为参考,$("#foo")将查找一个元素id="foo",其中应该只有一个元素,因为ID应该是唯一的.
$(".foo")将寻找class="foo"您可以拥有任意数量的元素.