jQuery 使用javascript或jquery按类名自动单击链接?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19426047/
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
Auto click a link by class name using javascript or jquery?
提问by user2889836
I am trying to auto click a link using a class name instead of the ID name.
我正在尝试使用类名而不是 ID 名称自动单击链接。
however my approach doesn't do anything!
但是我的方法没有做任何事情!
Here is what I have done:
这是我所做的:
<script type="text/javascript">
$(document).ready(function(){
document.getElementsByClassName("some-iclass").click();
});
</script>
Could someone point me in the right direction please?
有人能指出我正确的方向吗?
EDIT:
编辑:
I've used the following code and still doesn't work:
我已经使用了以下代码,但仍然无法正常工作:
<script type="text/javascript">
$(document).ready(function(){
$(".myLink").click();
});
</script>
<a class="myLink" href="http://yahoo.com"> CLICK HERE NOW </a>
and I have this right at the top of my page header:
我在我的页眉顶部有这个权利:
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
EIDT:
EIDT:
i've tried this as well and still doesn't work:
我也试过这个,但仍然不起作用:
<script type="text/javascript">
$(document).ready(function(){
$('.myLink').trigger('click');
});
</script>
回答by geevee
here you go:
干得好:
<script type="text/javascript">
$(function(){
$('.className').trigger('click');
});
</script>
hope that helps.
希望有帮助。
UPDATE:
更新:
try:
尝试:
<script type="text/javascript">
$(function(){
window.location.href = $('.className').attr('href');
});
</script>
after your edit, i think this is what you need.
在您编辑后,我认为这就是您所需要的。
回答by Denys Séguret
getElementsByClassName
doesn't return an element but a NodeList which may contain more than one element.
getElementsByClassName
不返回一个元素,而是一个可能包含多个元素的 NodeList。
You may do this :
你可以这样做:
document.getElementsByClassName("some-iclass")[0].click();
or if you want to click all elements :
或者如果你想点击所有元素:
var list = document.getElementsByClassName("some-iclass");
for (var i=0; i<list.length; i++) list[i].click();
But as you use jQuery, it would be simpler to do
但是当你使用 jQuery 时,它会更简单
$('.some-iclass').click();
but only when the click event handler was added with jQuery (in other cases, like for example in case of an href attribute, use the standard dom functions).
但仅当使用 jQuery 添加 click 事件处理程序时(在其他情况下,例如在 href 属性的情况下,使用标准 dom 函数)。
回答by user3206770
for auto-clicking a button or a link
用于自动单击按钮或链接
"<"body onload="document.getElementById('some-class')[0].click()" ">"
"<"body onload="document.getElementById('some-class')[0].click()" ">"
this works...:)
这有效...:)
回答by coolguy
$(document).ready(function(){
$(".some-iclass").trigger('click');
});
回答by griffon vulture
Simple with jquery $(".some-iclass").click();
用 jquery 简单 $(".some-iclass").click();
if you have a lot of elements with this class - point to the wanted element:
i.e. $($(".some-iclass")[0]).click();
如果你有很多这个类的元素 - 指向想要的元素:即 $($(".some-iclass")[0]).click();
回答by SidOfc
if you want to autoclick a link and you are using jQuery, you could use
如果你想自动点击一个链接并且你正在使用 jQuery,你可以使用
$('.yourClass').click();
if you need this to be one link in a collection of multiple links, you could do this:
如果您需要将其作为多个链接集合中的一个链接,您可以这样做:
$($('.yourClass')[0]).click();
Where 0 is the index of the element in the jQuery object.
其中 0 是 jQuery 对象中元素的索引。
document.getElementsByClassName('yourClass');
does not work in older browsers so it's best to use jQuery here for cross-browser compatibility.
document.getElementsByClassName('yourClass');
在旧浏览器中不起作用,因此最好在此处使用 jQuery 以实现跨浏览器兼容性。
回答by Naiguel Developer
For me, I managed to make it work that way. I deployed the automatic click in 5000 milliseconds and then closed the loop after 1000 milliseconds. Then there was only 1 automatic click.
对我来说,我设法让它以这种方式工作。我在 5000 毫秒内部署了自动点击,然后在 1000 毫秒后关闭了循环。然后只有 1 次自动点击。
<script>
var myVar = setInterval(function ({document.getElementById("test").click();}, 500);
setInterval(function () {clearInterval(myVar)}, 1000);
</script>