如何使用jQuery获取clicked元素的ID

时间:2021-06-14 20:33:52

I have the following html:

我有以下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:

以及以下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; 
    }); 

});

the 'pagerlink' links control are to jQuery Cycle slideshow. If I swap this line:

'pagerlink'链接控件是jQuery Cycle幻灯片。如果我交换这一行:

$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?

它的工作原理......(显然只能导航到7号幻灯片)。所以,我的问题是如何获取被点击链接的ID并将其传递到该行?

Thanks in advance!

提前致谢!

4 个解决方案

#1


58  

Your IDs are #1, and cycle just wants a number passed to it. You need to remove the # before calling cycle.

你的ID是#1,循环只想传递一个数字。您需要在调用周期之前删除#。

$('a.pagerlink').click(function() { 
    var id = $(this).attr('id');
    $container.cycle(id.replace('#', '')); 
    return false; 
});

Also, IDs shouldn't contain the # character, it's invalid (numeric IDs are also invalid). I suggest changing the ID to something like pager_1.

此外,ID不应包含#字符,它无效(数字ID也无效)。我建议将ID更改为pager_1。

<a href="#" id="pager_1" class="pagerlink" >link</a>

$('a.pagerlink').click(function() { 
    var id = $(this).attr('id');
    $container.cycle(id.replace('pager_', '')); 
    return false; 
});

#2


7  

You just need to remove the hash from the beginning:

您只需要从头开始删除哈希:

$('a.pagerlink').click(function() { 
    var id = $(this).attr('id').substring(1);
    $container.cycle(id); 
    return false; 
}); 

#3


3  

Your id will be passed through as #1, #2 etc. However, # is not valid as an ID (CSS selectors prefix IDs with #).

您的ID将作为#1,#2等传递。但是,#作为ID无效(带有#的CSS选择器前缀ID)。

#4


2  

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:

首先,除非您使用HTML5 DOCTYPE,否则您不能只为您的ID编号。其次,您需要删除每个ID中的#或用以下内容替换它:

$container.cycle(id.replace('#','')); 

#1


58  

Your IDs are #1, and cycle just wants a number passed to it. You need to remove the # before calling cycle.

你的ID是#1,循环只想传递一个数字。您需要在调用周期之前删除#。

$('a.pagerlink').click(function() { 
    var id = $(this).attr('id');
    $container.cycle(id.replace('#', '')); 
    return false; 
});

Also, IDs shouldn't contain the # character, it's invalid (numeric IDs are also invalid). I suggest changing the ID to something like pager_1.

此外,ID不应包含#字符,它无效(数字ID也无效)。我建议将ID更改为pager_1。

<a href="#" id="pager_1" class="pagerlink" >link</a>

$('a.pagerlink').click(function() { 
    var id = $(this).attr('id');
    $container.cycle(id.replace('pager_', '')); 
    return false; 
});

#2


7  

You just need to remove the hash from the beginning:

您只需要从头开始删除哈希:

$('a.pagerlink').click(function() { 
    var id = $(this).attr('id').substring(1);
    $container.cycle(id); 
    return false; 
}); 

#3


3  

Your id will be passed through as #1, #2 etc. However, # is not valid as an ID (CSS selectors prefix IDs with #).

您的ID将作为#1,#2等传递。但是,#作为ID无效(带有#的CSS选择器前缀ID)。

#4


2  

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:

首先,除非您使用HTML5 DOCTYPE,否则您不能只为您的ID编号。其次,您需要删除每个ID中的#或用以下内容替换它:

$container.cycle(id.replace('#',''));