简体   繁体   English

CSS在PHP MYSQL的子菜单菜单上不起作用

[英]CSS not working on menu with submenu in PHP MYSQL

I am trying to make a vertical drop-down menu with sub-menus, for example: 我正在尝试使用子菜单制作一个垂直下拉菜单,例如:

1-
 1-
 2-
  1-
3- 

I've come up with the function below, but the CSS is not being applied even though it is defined on the page using the following: 我想出了下面的函数,但是即使使用以下命令在页面上定义了CSS,也不会应用CSS:

<link href="estilo.css" rel="stylesheet" type="text/css" media="screen" />

What am I doing wrong? 我究竟做错了什么?

function display_menus($menuIdPai = 0) {
    $query = mysql_query("SELECT * FROM menu WHERE menuIdPai = ".$menuIdPai) or die(mysql_error());
    if (mysql_num_rows($query) > 0) {
        echo "<ul>";
        while ($row = mysql_fetch_array($query)) {
            echo "<li> <a href='percentagem.php'>".$row['id'].$row['menuNome']."</a>";
            display_menus($row['menuId']);
            echo "</li>";
        }
        echo "</ul>";
    }
}
<ul id="nav"> 
    <?php display_menus(); ?>
</ul>
ul {
    margin: 0;
    padding: 0;
    list-style: none;
    width: 150px;
}
ul li {
    position: relative;
}
li ul {
    position: absolute;
    left: 149px;
    top: 0;
    display: none;
}
ul li a {
    display: block;
    text-decoration: none;
    color: #E2144A;
    background: #fff;
    padding: 5px;
    border: 1px solid #ccc;
}
li:hover ul {
    display: block;
}

The CSS is not being applied because your HTML output is incorrect. 由于您的HTML输出不正确,因此未应用CSS。 In the following code: 在下面的代码中:

<ul id="nav"> 
    <?php display_menus(); ?>
</ul>

the call to display_menus() produces its own <ul>...</ul> tags, so you end up with: display_menus()的调用会产生自己的<ul>...</ul>标签,因此您最终得到:

<ul id="nav"><ul> ...
</ul></ul>

This is, of course, not valid HTML. 当然,这不是有效的HTML。 One solution to this is to skip the <ul>...</ul> tags in the HTML: 一种解决方案是跳过HTML中的<ul>...</ul>标签:

<?php display_menus(); ?>

and adjust display_menus() to output the id="nav" attribute only to the root menu (when $menuIdPai == 0 ): 并调整display_menus()以将id="nav"属性仅输出到根菜单(当$menuIdPai == 0 ):

function display_menus($menuIdPai = 0) {

/* Added $id variable: */

    $id = ($menuIdPai == 0 ? ' id="nav"' : ''); /* Note space before id */

    $query = mysql_query("SELECT * FROM menu WHERE menuIdPai = ".$menuIdPai) or die(mysql_error());
    if (mysql_num_rows($query) > 0) {

/* Added $id here: */

        echo "<ul$id>";

        while ($row = mysql_fetch_array($query)) {
            echo "<li> <a href='percentagem.php'>".$row['id'].$row['menuNome']."</a>";
            display_menus($row['menuId']);
            echo "</li>";
        }
        echo "</ul>";
    }
}

Note that you will also produce incorrect HTML if $row['id'] or $row['menuNome'] happens to contain < or & characters. 请注意,如果$row['id']$row['menuNome']恰巧包含<&字符,您也会生成不正确的HTML。 You should probably escape the latter with htmlspecialchars($row['menuNome']) . 您可能应该使用htmlspecialchars($row['menuNome'])逃避后者。 Probably not the first, depending on what kind of values you have for the id 's. 可能不是第一个,这取决于id的值类型。

Note also that the MySQL extension is deprecated and no longer maintained. 另请注意 ,不建议使用MySQL扩展,并且不再对其进行维护。 You should be using MySQLi or PDO these days. 这些天您应该使用MySQLiPDO

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

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