如何使用jquery获得clicked元素的ID? [英] How can I get the id of clicked element using jquery?

查看:101
本文介绍了如何使用jquery获得clicked元素的ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取被单击元素的ID,然后显示与该ID匹配的div.我正在使用以下代码,但无法正常工作.请帮忙.

I want to get the id of the clicked element and then show divs that match this id. I am using the following code, and it's not working. Please help.

$(function () {  

var tabContainers = $('div.difContetform > div');
    $('div#head-nav ul a').click(function (event) { 
        $('div#head-nav ul a').removeClass('current');
        $(this).addClass('current');
        var current_id = $(this).attr("id");
        var targeted='DIV'+current_id;
        $(targeted).show();
        $(targeted:not).hide(); 
        // 
        return false; 
     })
});

推荐答案

您要使用正确的选择器语法通过id(即字符串#id ...)抓取div,因此:

You want to use the right selector syntax to grab your divs by id, which is the string #id... Therefore:

 $('#'+targeted).show();
 $('something:not(#'+targeted+')').hide();    

编辑:再次查看(两次操作),您将无法隐藏所有不匹配的内容,因为它将隐藏您的整个页面.您需要确保仅选择DIV,而不要选择要显示的DIV.运作方式取决于您的页面布局(因此,在上面的示例中为something).

EDIT: Looking at this again (double-take), you can't just hide everything that doesn't match, as it will hide your whole page. You'll need to make sure you're selecting only DIVs, but not the one you want to show. How that works depends on your page layout (hence the something in the example above).

这篇关于如何使用jquery获得clicked元素的ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆