javascript - 显示和隐藏 div onclick

标签 javascript php mysql

我创建这段代码来显示和隐藏 div 这段代码有效,但是当我将它与 php 一起使用时,当我单击第一个 div 的显示时,他向我展示了 div 但是当我点击第二个时,他会告诉我第一个而不是第二个如何解决这个问题?

    include("connect.php");
    $sql=mysqli_query($conn,"select * from tbl_codelibrary order by db_id desc")or die(mysqli_error($conn));
  echo'<div id="no-more-tables">
            <table class="col-md-12 table-bordered table-striped table-condensed cf">
                <thead class="cf">';
echo"<tr>";
echo"<th style='background:#f7ac01;font-size:13px;vertical-align: middle;text-align:center' rowspan='2'>Title</th>
<th style='background:#f7ac01;font-size:13px;vertical-align: middle;text-align:center' rowspan='2'>Code</th>";
 echo"</tr></thead><tbody>";          
    while($row=mysqli_fetch_array($sql)){
        $title=$row['db_title'];
        $code=$row['db_code'];
echo"<tr>";
echo"<td data-title='Title'>";echo $title;echo'<div id="opener"><a href="#1" name="1">Show Code</a></div>';echo"<br/>";echo'<div id="upbutton"><a onclick="return hide();">Hide Code</a></div>'; echo"</td>";         
echo"<td data-title='Code'>";echo"<pre><code class='' >&lt;";?><?php echo $row['db_code'];?><?php echo"&gt;</code></pre>";echo"</td>";          
    }
    ?>
            </body>

        <script> 
            function show() { 
                if(document.getElementById('benefits').style.display=='none') { 
                    document.getElementById('benefits').style.display='block'; 
                } 
                return false;
            } 
            function hide() { 
                if(document.getElementById('benefits').style.display=='block') { 
                    document.getElementById('benefits').style.display='none'; 
                } 
                return false;
            }   
        </script> 

最佳答案

您正在使用固定 id 值而不是动态值的循环中创建 html。这会导致 id 冲突。因此,您的代码将无法按预期工作。这始终只采用页面的第一个 id。

我已经按照您的要求写了一些动态内容如下:

    <style>
    .buttonCode{
        border:1px solid #000;
        background-color:#ccc;
        border-radius:5px;
    }
    </style>
    <?php
    for($i=0;$i<3;$i++) {
        $title = "Title-".$i;
        $content = "Content-".$i;
        echo $title;
        echo'<br>';
        echo'<a class="showContent buttonCode">Show Code</a>';
        echo'&nbsp;&nbsp;';
        echo'<a class="hideContent buttonCode">Hide Code</a>'; 
        echo'<br>';
        echo"<pre><code class='benefits' style='display:none;'>&lt;".$content."&gt;</code></pre>";
        echo'<br>';
    }
    ?>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script>
    $('.showContent').on('click', function(e) {
    $(this).nextAll(':has(.benefits):first').find('.benefits').show();
    });
    $('.hideContent').on('click', function(e) {
    $(this).nextAll(':has(.benefits):first').find('.benefits').hide();
    });
    </script> 

希望对您有所帮助。

如果你只想使用javascript,那么你可以像下面那样做:

    <style>
    .buttonCode{
        border:1px solid #000;
        background-color:#ccc;
        border-radius:5px;
    }
    </style>
    <?php
    for($i=0;$i<3;$i++) {
        $title = "Title-".$i;
        $content = "Content-".$i;
        echo $title;
        echo'<br>';
        echo'<a class="buttonCode" onclick="showbenefit('.$i.')">Show Code</a>';
        echo'&nbsp;&nbsp;';
        echo'<a class="buttonCode" onclick="hidebenefit('.$i.')">Hide Code</a>'; 
        echo'<br>';
        echo"<pre><code class='benefits' id='benefits-".$i."' style='display:none;'>&lt;".$content."&gt;</code></pre>";
        echo'<br>';
    }
    ?>
    <script> 
    function showbenefit(s) { 
        if(document.getElementById('benefits-'+s).style.display=='none') { 
            document.getElementById('benefits-'+s).style.display='block'; 
        } 
        return false;
    } 
    function hidebenefit(h) { 
        if(document.getElementById('benefits-'+h).style.display=='block') { 
            document.getElementById('benefits-'+h).style.display='none'; 
        } 
        return false;
    }
    </script> 

关于javascript - 显示和隐藏 div onclick,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39659087/

相关文章:

javascript - 用 css 居中 sweetalert 文本

php - 发生错误 SQLSTATE[HY093] : Invalid parameter number: parameter was not defined

php - 连接两个数组的值

php - "add employee"没有页面重定向的表单

Mysql 将重复数据的行位置更改为列位置

javascript - Express 中间件中丢失 ajax 数据

javascript - 我试图通过使用 javascript 中的 FizzBu​​zz 问题来更好地理解递归

javascript - Jest : select an HTML element through innerHTML

php - 从 PHP 脚本执行时 MySQL 临时变量不起作用

php - 如何通过cakephp记录mysql错误?