jQuery 如何使用jQuery获取点击元素的ID

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7838238/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 00:20:03  来源:igfitidea点击:

How to get ID of clicked element with jQuery

jqueryjquery-cycle

提问by Adam

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!

提前致谢!

回答by Rocket Hazmat

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

您的 ID 是#1,并且cycle只想要一个数字传递给它。您需要#在调用之前删除cycle

$('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; 
});

回答by Richard Dalton

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; 
}); 

回答by David Neale

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 前加上 #)。

回答by Joseph Marikle

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('#','')); 

回答by Anurag M.

@Adam Just add a function using onClick="getId()"

@Adam 只需使用 onClick="getId()" 添加一个函数

function getId(){console.log(this.event.target.id)}