使用 jQuery 单击加载链接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10000435/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Click link on load with jQuery
提问by Paul
Here's a contrived example of my code:
这是我的代码的一个人为示例:
<!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>
I would like to have the link fired immediately upon page load. The above does not work and is incorrect? How could this be done?
我希望在页面加载时立即触发链接。以上不起作用,是不正确的?这怎么可能?
回答by Rory McCrossan
You have the class foo
set on your element, not an id
so you need to use .foo
. Also, as @JusticeErolin pointed out, the document ready handler doesn't need quotes. Try this:
您foo
在元素上设置了类,而不是 an,id
因此您需要使用.foo
. 此外,正如@JusticeErolin 指出的那样,文档就绪处理程序不需要引号。尝试这个:
$(document).ready(function() {
$(".foo").trigger('click');
});
For reference, $("#foo")
will look for an element with id="foo"
, of which there should only ever be one in your page as ids should be unique.
作为参考,$("#foo")
将查找带有 的元素id="foo"
,其中在您的页面中应该只有一个元素,因为 id 应该是唯一的。
$(".foo")
will look for elements with class="foo"
, of which you can have as many as you like.
$(".foo")
将查找带有 的元素class="foo"
,您可以拥有任意数量的元素。
回答by Irshad Khan
I tried many options, but I found one option which worked for me :
我尝试了很多选项,但我找到了一个对我有用的选项:
$(document).ready(function(){
$('#upload-file')[0].click(function(){
});
});
回答by user4255991
You have to wait until controls are loaded on the page
您必须等到控件加载到页面上
add this to the bottom on the page
将此添加到页面底部
<script>
$(document).ready(function(e){
$("#whateverElement").click();
});
</script>