I want to get the id of the clicked element and then show div
s that match this id. I am using the following code, and it's not working. Please help.
我想获取被点击元素的id,然后显示与此id匹配的div。我使用以下代码,它不起作用。请帮忙。
$(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;
})
});
2 个解决方案
#1
You want to use the right selector syntax to grab your divs by id, which is the string #id
... Therefore:
你想使用正确的选择器语法通过id抓取你的div,这是字符串#id ...因此:
$('#'+targeted).show();
$('something:not(#'+targeted+')').hide();
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).
编辑:再看一遍(双拍),你不能只隐藏所有不匹配的东西,因为它会隐藏你的整个页面。您需要确保只选择DIV,而不是选择要显示的DIV。如何工作取决于您的页面布局(因此上面的示例中的内容)。
#2
Thanks a lot dear now i am able to show the div but could't hide others. as you said all page disappear i have on container id=formContainer and other divs (child of that id div) under this id show hide and 1 shown that is clicked.
非常感谢亲爱的,我现在可以展示div但却无法隐藏其他人。正如你所说的所有页面消失我在容器id = formContainer和其他div(该id div的孩子)下这个id show hide和1显示被点击。
i am using below syntax
我使用以下语法
$('div#difContetform > div:not(#'+targeted+')').hide();
but its not working although page not disappear but not hiding other divs
但它没有工作,虽然页面没有消失,但没有隐藏其他div
#1
You want to use the right selector syntax to grab your divs by id, which is the string #id
... Therefore:
你想使用正确的选择器语法通过id抓取你的div,这是字符串#id ...因此:
$('#'+targeted).show();
$('something:not(#'+targeted+')').hide();
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).
编辑:再看一遍(双拍),你不能只隐藏所有不匹配的东西,因为它会隐藏你的整个页面。您需要确保只选择DIV,而不是选择要显示的DIV。如何工作取决于您的页面布局(因此上面的示例中的内容)。
#2
Thanks a lot dear now i am able to show the div but could't hide others. as you said all page disappear i have on container id=formContainer and other divs (child of that id div) under this id show hide and 1 shown that is clicked.
非常感谢亲爱的,我现在可以展示div但却无法隐藏其他人。正如你所说的所有页面消失我在容器id = formContainer和其他div(该id div的孩子)下这个id show hide和1显示被点击。
i am using below syntax
我使用以下语法
$('div#difContetform > div:not(#'+targeted+')').hide();
but its not working although page not disappear but not hiding other divs
但它没有工作,虽然页面没有消失,但没有隐藏其他div