单击列表成员时显示子项

Show childs when list member is clicked

本文关键字:显示 列表 成员 单击      更新时间:2023-09-26

我想用javascript做一些扩展/折叠列表。据我所知,我做得很好,但当我点击列表标题时,我会得到

未捕获的SyntaxError:意外的令牌}

在html代码的和处。

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Kaç Yakıyor?</title>
    <style type="text/css">
    body
    {
        background-color:#CCC;
    }
    h1.header
    {
        font-family:"Courier New", Courier, monospace;
        font-size:45px;
        font-style:oblique;
        font-weight:400;
        text-align:center;
    }
    div.content
    {
        margin-top:100px;
        margin-left:auto;
        margin-right:auto;
        width:61%;
        background-color:#F00;
    }
    div.vehicles
    {
        margin-top:100px;
        width:17%;
        height:200px;
        background-color:#0F6;
        float:left;
        overflow:auto;
    }
    li.c
    {
        font-weight:bold;
    }
    li
    {
        font-weight:normal;
        display:none;
    }
    </style>
    </head>
    <body>
    <?php
    mysql_connect("localhost","root","123123") or die(mysql_error());
    mysql_select_db("kacyakiyor");
    $query = "SELECT marka,model from arabalar";
    $result = mysql_query($query);
    ?>
            <h1 class="header">KAÇ YAKIYOR</h1>
            <hr width="500px"/>
            <div class="vehicles">
            <h2><strong><em>Araba</em></strong></h2><hr/>
    <?php
            while($row = mysql_fetch_array($result)){
                echo "<ul >";
                    echo "<li.c id='seen'><a href='#' onclick='show('hidden')'>".$row['marka']."</a>";
                        echo "<ul>";
                            echo "<li id='hidden'>".$row['model']."</li>";
                        echo "</ul>";
                echo "</ul>";       
            }    
    ?>
            <h2><strong><em>Motosiklet</em></strong></h2><hr/><br/></div>
            <div class="content">asdfasfda</div>
    <?php
    mysql_close();
    ?>
    <script type="text/javascript">
        function show(id){
            var hiddenElement = document.getElementById(id);
            if(hiddenElement.style.display == 'none')
            {
                hiddenElement.style.display = 'block';
            }
            else
            {
                hiddenElement.style.display = 'none';
            }
        }
    </script>
    </body>
    </html>

在这之后我得到了错误,但我没有看到任何额外的"}"。

<div class="content">asdfasfda</div>

更改此行

 echo "<li.c id='seen'><a href='#' onclick='show('hidden')'>".$row['marka']."</a>";

echo "<li.c id='seen'><a href='#' onclick='show('"hidden'")'>".$row['marka']."</a>";

更新

我注意到的一件事是,你在一个循环中使用元素id,这个循环将生成多个具有相同id的元素。你不能有多个具有同一id的元素,所以最好使用class并根据它们的类名来选择它们。

您在javascript中转义引号时遇到问题。

从更改此onclick

onclick='show('hidden')'

 onclick='show('"hidden'")'

除了其他海报所说的之外,您还需要更改这一行:

echo "<li.c id='seen'><a href='#' onclick='show('hidden')'>".$row['marka']."</a>";

echo "<li id='seen'><a href='#' onclick='show('"hidden'")'>".$row['marka']."</a>";这个

我能想到的唯一一件事是,你的数据中是否包含一个}。你可能需要做一个htmlentities($row['marka'])

我会使用jQuery子级:

    $("li.c a").click(function(){
    var children = $(this).children();
    if(children.filter(":visible").length > 0)
    children.hide();
    else children.show();
//or do something you like
    });

如果你需要更多的解释,请告诉我。

第页。S.:李-你是说li class='c'吗?

您也可以将jQueryUI手风琴用于类似的目的。