简体   繁体   English

使用javascript显示和隐藏动态生成的ID

[英]Showing and hiding dynamically generated ID's with javascript

The code below is showing and hiding div content with dynamically generated id's like div_1, div_2 from div id , all seems working fine except that it requires to hide one div content at a time like clicking div_1 should open its content and on clicking div_2 should hide div_1. 下面的代码显示和隐藏DIV的内容dynamically generated id's喜欢div_1, div_2div id ,一切似乎工作除了它需要像点击div_1应当公开的内容和点击div_2应隐藏时隐藏一个DIV内容精div_1。 please help me sort out this problem. 请帮助我解决这个问题。

echo "<span class='bold'><a name='form_a_$group_seq' href='#div_$group_seq' id='form_a_$group_seq' value='1' " .
"onclick='return divclick(this,\"div_$group_seq\");'";
 if ($display_style == 'block') echo "clicked";
 echo "<b>" . xl_layout_label($group_name) . "</b></a></span>\n";

 echo "<div id='div_$group_seq' class='section' style='display:$display_style;'>\n";
 echo " <table border='0' cellpadding='0'>\n";
 $display_style = 'none';
}
else if (strlen($last_group) == 0) {
echo " <table border='0' cellpadding='0'>\n";
}

Below is the javascript to make the code workable. 下面是使代码可行的JavaScript。 but its showing or hiding all the div contents at a time. 但一次显示或隐藏所有div内容。

function divclick(a, divid) {
    var divstyle = document.getElementById(divid).style;
    if ( divstyle.display == 'none' ) {
        divstyle.display = 'block';
    } else {
        divstyle.display = 'none';
    }
    return true;
}

this is a piece of updated html code that's what browser is rendering. 这是浏览器正在渲染的一段更新的html代码。

<div class='container2'><ul class='taby'><li class='dropown'><a name='form_a_1' href='#div_1'   id='form_a_1' value='1' onclick='return divclick(this,"div_1");'>Who</a></li></ul>
<div id='div_1' class='section'>
<table border='0' cellpadding='0'>
<div id='div_2' class='section'>
<table border='0' cellpadding='0'>
<div id='div_3' class='section'>
<table border='0' cellpadding='0'>

This is really too messy to work with and understand. 这实在太混乱了,无法使用和理解。 You should consider cleaning up the way you dynamically render html, and it would make problems like this a lot easier to solve when you ran into them. 您应该考虑清理动态呈现html的方式,当您遇到此类问题时,这样的问题将更容易解决。 Or prevent them all together. 或一起阻止它们。

<span class='bold' style='background:#0DCAD1'>


<a name="<?php echo "form_a_$group_seq";?>" href="<?php echo "#div_$group_seq";?>" id="<?php echo "form_a_$group_seq";?>" value='1'>

etc.... 等等....

If you render all of your html in echo strings, you'll find it very hard to deal with later on. 如果以回显字符串形式呈现所有html,则以后将很难处理。 Especially, when you're working on applications which large amounts of rendering. 特别是在处理大量渲染的应用程序时。

If you are using jquery in the page you can add one line in your code 如果您在页面中使用jQuery,则可以在代码中添加一行

function divclick(a, divid) {
    var divstyle = document.getElementById(divid).style;
    if ( divstyle.display == 'none' ) {
        $(".section").css('display', 'none');
        divstyle.display = 'block';
    } else {
        divstyle.display = 'none';
    }
    return true;
}

If you are not using jquery on the page it gets a little more complicated but can be done with only adding a few lines. 如果您不在页面上使用jquery,它会变得有些复杂,但只需添加几行即可完成。

function divclick(a, divid) {
    var divstyle = document.getElementById(divid).style;
    if ( divstyle.display == 'none' ) {
        var sections = document.getElementsByClassName('section');
        Array.prototype.forEach.call(sections, function(section){
            section.style.display = 'none';
        });
        divstyle.display = 'block';
    } else {
        divstyle.display = 'none';
    }
    return true;
}

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

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