【问题标题】:javascript strange malfunctionjavascript奇怪的故障
【发布时间】:2012-06-07 21:56:52
【问题描述】:

我有一个由 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
}
}();

感谢您的宝贵时间

【问题讨论】:

  • 那个javascript被混淆了

标签: php javascript include


【解决方案1】:

在你忘记的 index.php 页面上

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

把它放在otherpage.php的底部或放在一个footer.php中以包含在页面底部。

【讨论】:

    【解决方案2】:

    好像第二页(不是index.php)没有

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

    所以没有创建菜单。

    【讨论】:

      【解决方案3】:

      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,你可以看到它没有被命中,因此从未被调用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-02-22
        • 1970-01-01
        • 1970-01-01
        • 2020-08-03
        • 1970-01-01
        • 2019-11-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多