0

这是我第二次尝试这个问题,所以请多多包涵。

如何将 JS 菜单包含到 php 页面中?

每当我为其中包含 html 的 nav 执行包含语句时,什么都不会显示。

index.php 有一个 include 语句,它应该从导航中获取代码并使用 menu.js 显示它

这是索引中的片段

    <span id="container" >

        <!-- NAVIGATION -->
        <ul id="menu">

<?php include('nav.php')?>
            </ul>
     </span>

这是nav.php

 <li><a href="index.php" title="Portraiture Gallery" class="selected">Portraiture Gallery</a>
            <ul>
            <li><a href="portraiture/adults.php" title="Adults">Adults  </a></li>
            <li><a href="portraiture/seniors.php" title="Seniors">Seniors  </a></li>
            <li><a href="portraiture/infantsandchildren.php" title="InfantsandChildren">Infants and Children  </a></li>
            <li><a href="portraiture/multiples.php" title="Multiples">Mutiples  </a></li>
            <li><a href="portraiture/hisangels.php" title="Hisangels">His Angels </a></li>

            </ul>
            </li>

遵循这种格式是因为 menu.js 就是为此而编写的。

菜单.js

 // JavaScript Document

    // DropDownMenu by Miha Hribar
    // http://hribar.info

    function addLoadEvent(func) {
        var oldonload = window.onload;
        if (typeof window.onload != 'function') {
            window.onload = func;
        } else {
            window.onload = function() {
                oldonload();
                func();
            }
        }
    }

    function prepareMenu() {
        // first lets make sure the browser understands the DOM methods we will be using
        if (!document.getElementsByTagName) return false;
        if (!document.getElementById) return false;

        // lets make sure the element exists
        if (!document.getElementById("menu")) return false;
        var menu = document.getElementById("menu");

        // for each of the li on the root level check if the element has any children
        // if so append a function that makes the element appear when hovered over
        var root_li = menu.getElementsByTagName("li");
        for (var i = 0; i < root_li.length; i++) {
            var li = root_li[i];
            // search for children
            var child_ul = li.getElementsByTagName("ul");
            if (child_ul.length >= 1) {
                // we have children - append hover function to the parent
                li.onmouseover = function () {
                    if (!this.getElementsByTagName("ul")) return false;
                    var ul = this.getElementsByTagName("ul");
                    ul[0].style.display = "block";
                    return true;
                }
                li.onmouseout = function () {
                    if (!this.getElementsByTagName("ul")) return false;
                    var ul = this.getElementsByTagName("ul");
                    ul[0].style.display = "none";
                    return true;
                }
            }
        }

        return true;
    }

    addLoadEvent(prepareMenu);
4

1 回答 1

0

您是否检查过 PHP 在运行时可能生成的任何错误消息?

并非所有 PHP 错误消息都会显示在浏览器中(取决于您的 HTTP 配置),因此检查通常位于 access.log 文件旁边的 error.log 文件总是有帮助的。

于 2012-06-29T17:51:58.463 回答