简体   繁体   English

jQuery-无法触发点击

[英]jQuery - Having trouble triggering a click

I think this should be an easy one but it's been confusing me for a few hours. 我认为这应该很容易,但是已经使我困惑了几个小时。 I kinds of get why it doesn't work, but at the same time I don't. 我有点明白为什么它不起作用,但同时我却不起作用。

So.. 所以..

This sets the first thumbs rel value 设置第一个拇指的rel值

var firstThumb = "01"

This is using the firstThumb variable to get the first thumb by its rel attr 这是使用firstThumb变量通过其rel attr获取第一个拇指

var getThumb   = $('#thumbs a').attr('rel' + firstThumb);

I've set "firstThumb" as a variable as I intend to increase it after each triggered click so it goes on to 02, 03, 04 etc.... For anybody wondering why I've done it that way. 我已将“ firstThumb”设置为变量,因为我打算在每次触发单击后增加它,因此它会继续显示为02、03、04等。对于任何想知道我为什么这样做的人。

So the result of getThumb SHOULD be 所以getThumb的结果应该是

$('#thumbs a').attr('rel', '01');

unless I've done something wrong? 除非我做错了什么?

NOW, once I've retrieved this - I want to click the #thumb a with a rel attribute of 01 现在,一旦检索到此内容,我想单击rel属性为01的#thumb

I've been playing around for a while and I keep coming back to this: 我已经玩了一段时间了,我一直回到这个话题:

getThumb.trigger("click");

This is essentially 这本质上是

$('#thumbs a').attr('rel', '01').trigger("click);

Can anybody see what I've done wrong here, as I say it's probably quite simple but I'm new to jQuery and I'm struggling :) 任何人都可以在这里看到我做错了什么,因为我说这可能很简单,但是我是jQuery的新手,而且正在苦苦挣扎:)

Full set of above code with interval 全套上述代码(带间隔)

var firstThumb = "01"
var getThumb   = $('#thumbs a').attr('rel' + firstThumb);

setInterval(function(){
   getThumb.trigger("click");
   thumbIncrease;
}, 1000);

For clarity, thumbIncrease is a function I've not written yet that after the first triggered click, the firstThumb variable is increased by 1, so it clicks the next thumbnail. 为了清楚起见,thumbIncrease是我尚未编写的函数,在第一次触发单击后,将firstThumb变量增加1,因此它单击了下一个缩略图。

Thanks for any help! 谢谢你的帮助!

I've fiddled it here if it helps: 我在这里弄弄了它是否有帮助:

http://jsfiddle.net/w235svyL/1/ http://jsfiddle.net/w235svyL/1/

Thanks!! 谢谢!!

$('#thumbs a').attr('rel' + firstThumb);

returns the value of attribute which matches 'rel' + firstThumb 返回与'rel' + firstThumb匹配的属性的值

To access the element, you should use 要访问元素,您应该使用

$('#thumbs a[rel="'+firstThumb+'"]');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM