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

发布于 2024-12-10 18:20:38 字数 824 浏览 0 评论 0原文

我有以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

我很OK 2024-12-17 18:20:38

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

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

另外,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; 
});

Your IDs are #1, and cycle just wants a number passed to it. You need to remove the # before calling 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.

<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; 
});
面犯桃花 2024-12-17 18:20:38

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

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

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; 
}); 
相思故 2024-12-17 18:20:38

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

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

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

function getId(){console.log(this.event.target.id)}
权谋诡计 2024-12-17 18:20:38

您的 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 #).

人间☆小暴躁 2024-12-17 18:20:38

首先,除非您使用 HTML5 DOCTYPE,否则您的 ID 不能只有数字。其次,您需要删除每个 id 中的 # 或将其替换为:

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

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:

$container.cycle(id.replace('#','')); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文