如何使用 jQuery 获取点击元素的 ID
我有以下 html:
<a href="#" id="#1" class="pagerlink" >link</a>
<a href="#" id="#3" class="pagerlink" >link</a>
<a href="#" id="#2" class="pagerlink" >link</a>
/*etc.... */
和以下 jQuery 脚本:
$(document).ready(function() {
var $container = $('.gallery_r').cycle({
fx: 'scrollHorz',
speed: 500,
timeout: 0
});
$('a.pagerlink').click(function() {
var id = $(this).attr('id');
$container.cycle(id);
return false;
});
});
“pagerlink”链接控件指向 jQuery Cycle 幻灯片。如果我交换这一行:
$container.cycle(id);
对于这个
$container.cycle(7);
它有效。 (显然只是导航到第 7 号幻灯片)。所以,我的问题是如何获取被单击链接的 ID 并将其传递到该行?
I have the following html:
<a href="#" id="#1" class="pagerlink" >link</a>
<a href="#" id="#3" class="pagerlink" >link</a>
<a href="#" id="#2" class="pagerlink" >link</a>
/*etc.... */
and the following jQuery script:
$(document).ready(function() {
var $container = $('.gallery_r').cycle({
fx: 'scrollHorz',
speed: 500,
timeout: 0
});
$('a.pagerlink').click(function() {
var id = $(this).attr('id');
$container.cycle(id);
return false;
});
});
the 'pagerlink' links control are to jQuery Cycle slideshow. If I swap this line:
$container.cycle(id);
for this
$container.cycle(7);
It works. (obviously only navigating to slide number 7). So, my question is how can I pick up the ID of the link being clicked and pass it into that line?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您的 ID 是
#1
,而cycle
只需要传递一个数字给它。您需要在调用cycle
之前删除#
。另外,ID 不应包含
#
字符,它是无效的(数字 ID 也无效)。我建议将 ID 更改为pager_1
之类的内容。Your IDs are
#1
, andcycle
just wants a number passed to it. You need to remove the#
before callingcycle
.Also, IDs shouldn't contain the
#
character, it's invalid (numeric IDs are also invalid). I suggest changing the ID to something likepager_1
.您只需从头开始删除哈希:
You just need to remove the hash from the beginning:
@Adam 只需使用 onClick="getId()" 添加一个函数
@Adam Just add a function using onClick="getId()"
您的 ID 将作为 #1、#2 等传递。但是,# 作为 ID 无效(CSS 选择器在 ID 前加上 #)。
Your id will be passed through as #1, #2 etc. However, # is not valid as an ID (CSS selectors prefix IDs with #).
首先,除非您使用 HTML5 DOCTYPE,否则您的 ID 不能只有数字。其次,您需要删除每个 id 中的 # 或将其替换为:
First off you can't have just a number for your id unless you are using the HTML5 DOCTYPE. Secondly, you need to either remove the # in each id or replace it with this: