简体   繁体   English

如何通过动态ID显​​示div?

[英]How to show div by dynamic id?

I've got the follow code我有以下代码

<ul>
    <li><a href="#" class="chooserClass" id="1">Google</a></li>
    <li><a href="#" class="chooserClass" id="2">Facebook</a></li>
</ul>

<!- hidden divs ->
<div id="1" style="display:none;">Email</div>
<div id="2" style="display:none;"">Social Network</div>

I tried the following code but when I click on a link i would like it to show a div based on the id of the link but it is not working我尝试了以下代码,但是当我单击链接时,我希望它根据链接的 id 显示一个 div,但它不起作用

<script type="text/javascript">
$(document).ready(function()
{
    $(".chooserClass").click(function() {
        var show = $(this).attr('id');
        $(show).show();
    });
});
</script>

Change改变

var show = $(this).attr('id');
$(show).show();

to

var show = $(this).attr('id');
$('#'+ show).show();

You have 2 elements on your page that have th same id, and they are numeric which from experience can cause all kinds of undesired behavior.您的页面上有 2 个元素具有相同的 id,它们是数字,根据经验可能会导致各种不良行为。

The way you want to this is like so:你想要的方式是这样的:

<ul>
    <li><a href="#" class="chooserClass" data-show="element1">Google</a></li>
    <li><a href="#" class="chooserClass" data-show="element2">Facebook</a></li>
</ul>

<!- hidden divs ->
<div id="element1" style="display:none;">Email</div>
<div id="element2" style="display:none;">Social Network</div>

We use a data-show element to store the value of the id of the element that we want to display, in the javascript we can now do this:我们使用一个 data-show 元素来存储我们想要显示的元素的 id 值,在 javascript 中我们现在可以这样做:

<script type="text/javascript">
$(document).ready(function()
{
    $(".chooserClass").click(function() {
        var show = $(this).data('show');
        $('#' + show).show();
    });
});
</script>

also note that I am added in the # (for id) in front of the variable when calling the show()另请注意,在调用show()时,我在变量前添加了 #(for id show()

Fiddle demo: http://jsfiddle.net/5U8sW/小提琴演示: http : //jsfiddle.net/5U8sW/

Well, here's a quick an dirty answer:好吧,这是一个快速而肮脏的答案:

The problem is that you can't have duplicate id's on DOM elements.问题是你不能在 DOM 元素上有重复的 id。 They should be unique.它们应该是独一无二的。 You can use a data- property to do what you are wanting.您可以使用数据属性来执行您想要的操作。

http://jsfiddle.net/725XT/ http://jsfiddle.net/725XT/

<ul>
    <li><a href="#" class="chooserClass" data-div-id="1">Google</a></li>
    <li><a href="#" class="chooserClass" data-div-id="2">Facebook</a></li>
</ul>

<!- hidden divs ->
<div id="1" style="display:none;">Email</div>
<div id="2" style="display:none;">Social Network</div>

And your Javascript:还有你的 Javascript:

$(document).ready(function()
{
    $(".chooserClass").click(function() {
        var show = $(this).data('div-id');
        console.log(show);
        $('#' + show).show();
    });
});

When using javascript or jQuery to search for elements with the same IDs, only the first element is picked.使用 javascript 或 jQuery 搜索具有相同 ID 的元素时,只会选取第一个元素。 I suggest you rename the IDs of the elements to be shown.我建议您重命名要显示的元素的 ID。 Take the code below for example.以下面的代码为例。

HTML HTML

<ul>
    <li><a href="#" class="chooserClass" id="1">Google</a></li>
    <li><a href="#" class="chooserClass" id="2">Facebook</a></li>
</ul>

<!- hidden divs ->
<div id="d_1" style="display:none;">Email</div>
<div id="d_2" style="display:none;"">Social Network</div>

jQuery jQuery

<script type="text/javascript">
$(document).ready(function()
{
    $(".chooserClass").click(function() {
        var id = $(this).attr('id');
        $('#d_'+id).show();
    });
});
</script>

jsFiddle js小提琴

First of all, your IDs should be unique.首先,您的 ID 应该是唯一的。 Thus, the <div> and the <a> must not share the similar ID names.因此, <div><a>不得共享相似的 ID 名称。 Additionally, AFAIK, in HTML4.1 IDs starting with digits are prohibited(?)此外,AFAIK,在 HTML4.1 中,禁止以数字开头的 ID(?)

Try the sample I coded above.试试我上面编码的示例。 Since you are using anchors, referencing the 'href' attribute would be more semantically correct.由于您使用的是锚点,因此引用 'href' 属性在语义上会更正确。

Cheers!干杯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM