javascript奇怪的故障

时间:2012-06-03 13:55:29

标签: php javascript include

我有一个由index.php和otherpage.php组成的网站。这两个页面都使用

include_once("header.inc")

header.inc实现了像这样的jscript文件

<script type="text/javascript" src="script.js"></script>

jscript文件让我使用漂亮的下拉菜单。

问题是菜单只能在index.php上正常工作,而不能在otherpage.php

上正常工作

真正让我感到的是,在otherpage.php上,并不是菜单根本无法正常工作,它只是部分无效。菜单将突出显示但不会下拉菜单。

你可以亲自看看

index.php

otherpage.php

有什么关于在我应该知道的PHP页面之间共享jscript文件的东西吗?

以下是菜单的相关jscript内容:

var menu = function() {
var t = 15, z = 50, s = 6, a;
function dd(n) {
    this.n = n;
    this.h = [];
    this.c = []
}


dd.prototype.init = function(p, c) {
    a = c;
    var w = document.getElementById(p), s = w.getElementsByTagName('ul'), l = s.length, i = 0;
    for(i; i < l; i++) {
        var h = s[i].parentNode;
        this.h[i] = h;
        this.c[i] = s[i];
        h.onmouseover = new Function(this.n + '.st(' + i + ',true)');
        h.onmouseout = new Function(this.n + '.st(' + i + ')');
    }
}
dd.prototype.st = function(x, f) {
    var c = this.c[x], h = this.h[x], p = h.getElementsByTagName('a')[0];
    clearInterval(c.t);
    c.style.overflow = 'hidden';
    if(f) {
        p.className += ' ' + a;
        if(!c.mh) {
            c.style.display = 'block';
            c.style.height = '';
            c.mh = c.offsetHeight;
            c.style.height = 0
        }
        if(c.mh == c.offsetHeight) {
            c.style.overflow = 'visible'
        } else {
            c.style.zIndex = z;
            z++;
            c.t = setInterval(function() {
                sl(c, 1)
            }, t)
        }
    } else {
        p.className = p.className.replace(a, '');
        c.t = setInterval(function() {
            sl(c, -1)
        }, t)
    }
}
function sl(c, f) {
    var h = c.offsetHeight;
    if((h <= 0 && f != 1) || (h >= c.mh && f == 1)) {
        if(f == 1) {
            c.style.filter = '';
            c.style.opacity = 1;
            c.style.overflow = 'visible'
        }
        clearInterval(c.t);
        return
    }
    var d = (f == 1) ? Math.ceil((c.mh - h) / s) : Math.ceil(h / s), o = h / c.mh;
    c.style.opacity = o;
    c.style.filter = 'alpha(opacity=' + (o * 100) + ')';
    c.style.height = h + (d * f) + 'px'
}

return {
    dd : dd
}
}();

感谢您的时间

3 个答案:

答案 0 :(得分:2)

在index.php页面上你忘记了

<script type="text/javascript">
    var menu = new menu.dd("menu");
    menu.init("menu", "menuhover");
</script>

将它放在otherpage.php的底部,或者将其放在footer.php中,以包含在页面底部。

答案 1 :(得分:1)

似乎第二页(不是index.php)没有

    <script type="text/javascript">
        var menu = new menu.dd("menu");
        menu.init("menu", "menuhover");
    </script>

因此未创建菜单。

答案 2 :(得分:1)

index.php在初始化菜单的页面末尾包含此代码:

    <script type="text/javascript">
        var menu = new menu.dd("menu");
        menu.init("menu", "menuhover");
    </script>

otherpage.php不包含该代码,因此代码永远不会被初始化并连接到您的HTML。

顺便说一句,您可以通过在代码中的.init()方法中放置断点来自行调试此类问题。在index.php中,您会看到断点被击中,如果您查看调用堆栈,您可以看到它的调用位置。如果你在otherpage.php中放置相同的断点,你可以看到它没有被击中,因此从未被调用过。