JavaScript 不会执行

标签 javascript html

我正在开发一个新网站。我以前写过很多代码,但没有这一篇那么先进。 我有一个 html 文件,它是主页。现在,我正在通过ajax更改页面的内容。一切正常。因此,当我单击“Home”菜单时,它会调用该函数,并获取另一个包含内容的 HTML 文件。

在我的主 html 中我有这个:

<html>
    <head>
    <title>
        Estancia
    </title>
    <link rel="stylesheet" href="css/stylesheet.css"/>
    <link rel="stylesheet" href="webfont/webFontStylesheet.css" type="text/css" charset="utf-8"/>
    <script type="text/javascript" src="js/ajax.js"></script>
</head>
<body onLoad = "javascript:home()">
    <div id = "bodyLeft">
        <!-- This is just a sideskirt -->
    </div>
    <div id = "bodyRight">
        <!-- This is just a sideskirt -->
    </div>

    <div id = "header">
        <h1>
            Estancia
        </h1>

    </div>
    <div id = "footer">
        <h5>
            Copyright&copy; 2013
        </h5>
    </div>
    <div id = "background">
        <!-- This is just the background color -->
    </div>
    <div id = "scrollCover">
        <!-- Used to cover the scrolled content -->
    </div>
    <div id = "body">

    </div>
    <div id = "content">
        If you see this then the page did not load correctly. Try reloading the page. If problem persists, try reinstalling the Mozilla Firefox Browser. <br/><br/> Error: Javascript may not be working. <br/> Fix: Enable Javascript in your browser.
    </div>
    <div id = "menu">
        <div id = "home" onClick="javascript:home()">
            <a href = "#"> Home </a>
        </div>
        <div id = "information">
            <a href = "#"> Information </a>
            <span id = "aboutDrop">
                <a class = "about" href="#" onClick = "javascript:about()"> About </a>
                <br/>
                <a class = "map" href="#" onClick = "javascript:map()"> Map </a>
            </span>
        </div>
        <div id = "history" onClick = "javascript:history()">
            <a href = "#"> History </a>
        </div>
        <div id = "pictureGallary">
            <a href = "#"> Picture Gallary </a>
            <span id = "pictureGalleryDrop">
                <a class = "ef" href = "#" onClick = "javascript:estanciaFarm()"> Estancia Farm </a>
                <br/>
                <a class = "ca" href = "#" onClick = "javascript:cattle()"> Cattle </a>
                <br/>
                <a class = "ga"href = "#" onClick = "javascript:game()"> Game </a>
                <br/>
                <a class = "go" href = "#" onClick = "javascript:goats()"> Goats </a>
                <br/>
                <a class = "sh" href = "#" onClick = "javascript:sheep()"> Sheep </a>
            </span>
        </div>
    </div>
</body>

这里没有问题...当我单击它时,它会正确加载。 因此,当单击 Home 时,它​​会通过 ajax 更改内容,如下所示:

var xmlhttp = new XMLHttpRequest();
    var path;
    var div;

function home()
{
    path = "html/home.html";
    div = "content";
    AJAX(path, div);
}

    function AJAX(path, div)
{
    xmlhttp.open("GET", path, false);
    xmlhttp.send();
    if (xmlhttp.readyState == 4)
    {
        document.getElementById(div).innerHTML = xmlhttp.responseText;
    }
}

正如您所看到的,它将另一个 html 文件加载到内容上。 所以,这是该 html 文件的代码:

<html>
<head>
    <link rel="stylesheet" href="css/homeMenu.css"/>
    <script type="text/javascript" src="js/javascript.js"></script>
</head>
<body>
    <div id = "slideshow">
        <div id = "first">
            <img id = "a1" src = "images/home/img1_e.jpg"/>
            <img id = "a2" src = "images/home/img2_e.jpg"/>
            <img id = "a3" src = "images/home/img3_e.jpg"/>
            <img id = "a4" src = "images/home/img4_e.jpg"/>
        </div>
        <div id = "second">
            <img id = "b4" src = "images/home/img4_e.jpg"/>
            <img id = "b1" src = "images/home/img1_e.jpg"/>
            <img id = "b2" src = "images/home/img2_e.jpg"/>
            <img id = "b3" src = "images/home/img3_e.jpg"/>
        </div>
        <div id = "third">
            <img id = "c3" src = "images/home/img3_e.jpg"/>
            <img id = "c4" src = "images/home/img4_e.jpg"/>
            <img id = "c1" src = "images/home/img1_e.jpg"/>
            <img id = "c2" src = "images/home/img2_e.jpg"/>

        </div>
        <div id = "fourth">
            <img id = "d2" src = "images/home/img2_e.jpg"/>
            <img id = "d3" src = "images/home/img3_e.jpg"/>
            <img id = "d4" src = "images/home/img4_e.jpg"/>
            <img id = "d1" src = "images/home/img1_e.jpg"/>
        </div>
    </div>
    </body>

当我单独加载这个 html 文件时,它工作得很好。我需要更改的只是将“../”添加到每个图像路径的开头。我相信你会知道为什么。快速描述是,如果它单独存在,它如果在另一个文件中,那么它需要返回一次。当它通过ajax加载到另一个主要的html中时,它位于每个文件的外部并直接进入其中。好的,这就清楚了。

问题...我面临的唯一问题是,当我单独加载 home.html 文件时,它会像小菜一碟一样执行其 javascript。

但是,当它加载到主 html 中时,它不起作用。它没有办法执行。我已经尝试过内部 JavaScript,但它仍然不起作用。

这是 JavaScript:

var useBSNns;

        if (useBSNns)
        {
            if (typeof(bsn) == "undefined")
                bsn = {}
            var _bsn = bsn;
        }
        else
        {
            var _bsn = this;
        }

        _bsn.Crossfader = function (divs, fadetime, delay )
        {   
            this.nAct = -1;
            this.aDivs = divs;

            for (var i=0;i<divs.length;i++)
            {
                document.getElementById(divs[i]).style.opacity = 0;
                document.getElementById(divs[i]).style.position = "absolute";
                document.getElementById(divs[i]).style.filter = "alpha(opacity=0)";
                document.getElementById(divs[i]).style.visibility = "hidden";
            }

            this.nDur = fadetime;
            this.nDelay = delay;

            this._newfade();
        }

        _bsn.Crossfader.prototype._newfade = function()
        {
            if (this.nID1)
                clearInterval(this.nID1);

            this.nOldAct = this.nAct;
            this.nAct++;
            if (!this.aDivs[this.nAct]) this.nAct = 0;

            if (this.nAct == this.nOldAct)
                return false;

            document.getElementById( this.aDivs[this.nAct] ).style.visibility = "visible";

            this.nInt = 50;
            this.nTime = 0;

            var p=this;
            this.nID2 = setInterval(function() { p._fade() }, this.nInt);
        }

        _bsn.Crossfader.prototype._fade = function()
        {
            this.nTime += this.nInt;

            var ieop = Math.round( this._easeInOut(this.nTime, 0, 1, this.nDur) * 100 );
            var op = ieop / 100;
            document.getElementById( this.aDivs[this.nAct] ).style.opacity = op;
            document.getElementById( this.aDivs[this.nAct] ).style.filter = "alpha(opacity="+ieop+")";

            if (this.nOldAct > -1)
            {
                document.getElementById( this.aDivs[this.nOldAct] ).style.opacity = 1 - op;
                document.getElementById( this.aDivs[this.nOldAct] ).style.filter = "alpha(opacity="+(100 - ieop)+")";
            }

            if (this.nTime == this.nDur)
            {
                clearInterval( this.nID2 );

                if (this.nOldAct > -1)
                    document.getElementById( this.aDivs[this.nOldAct] ).style.visibility = "hidden";    

                var p=this;
                this.nID1 = setInterval(function() { p._newfade() }, this.nDelay);
            }
        }

        _bsn.Crossfader.prototype._easeInOut = function(t,b,c,d)
        {
            return c/2 * (1 - Math.cos(Math.PI*t/d)) + b;
        }

请问,当 home.html 加载到主 html 中时它不想执行,但当 home.html 单独执行时它可以工作,这是怎么回事?

最佳答案

我知道您已经在现有的东西上投入了大量的工作,但是研究一下 jQuery 插件(例如 Cycle)可能会有所帮助。 http://malsup.com/jquery/cycle

<小时/>

另一方面,您需要删除 <script type="text/javascript" src="js/javascript.js"></script>来自Home.html您正在检索的页面并将其放在 main.html 的头部您正在从中加载辅助页面的文件。它将被调用,而不是通过 AJAX 加载内联执行,这是您遇到的问题。所以我们将其移至 main.html当我们的 home.html 时,我们将手动调用它页面已加载。

我改变了你的AJAX AJAX 成功时采用回调方法或函数的方法。您将看到 AJAX 的第三个参数method 是完成后调用的函数,在我们的例子中是 startBSN你会看到我把你的 js/javascript 包裹在哪里使用此方法可以进一步发挥作用。

var xmlhttp = new XMLHttpRequest();
var path;
var div;

function home()
{
   path = "html/home.html";
   div = "content";
   AJAX(path, div, 'startBSN');
}

function AJAX(path, div, callback)
{
    xmlhttp.open("GET", path, false);
    xmlhttp.send();
    if (xmlhttp.readyState == 4)
    {
       document.getElementById(div).innerHTML = xmlhttp.responseText;
       window[callback]();
    }
}

我已经删除了你的js/javascript.js并将其包含在main.html的头部中,您应该将其从 home.html 中删除

我还包装了你的js/javascript.js将代码写入名为 startBSN 的可调用函数中否则它将在页面加载时作为匿名函数运行。

这是您的js.javascript具有周围函数的文件 startBSN

function startBSN(){
    var useBSNns;

    if (useBSNns)
    {
        if (typeof(bsn) == "undefined")
            bsn = {}
        var _bsn = bsn;
    }
    else
    {
        var _bsn = this;
    }


    _bsn.Crossfader = function (divs, fadetime, delay )
    {   
        this.nAct = -1;
        this.aDivs = divs;

        for (var i=0;i<divs.length;i++)
        {
            document.getElementById(divs[i]).style.opacity = 0;
            document.getElementById(divs[i]).style.position = "absolute";
            document.getElementById(divs[i]).style.filter = "alpha(opacity=0)";
            document.getElementById(divs[i]).style.visibility = "hidden";
        }

        this.nDur = fadetime;
        this.nDelay = delay;

        this._newfade();
    }

    _bsn.Crossfader.prototype._newfade = function()
    {
        if (this.nID1)
            clearInterval(this.nID1);

        this.nOldAct = this.nAct;
        this.nAct++;
        if (!this.aDivs[this.nAct]) this.nAct = 0;

        if (this.nAct == this.nOldAct)
            return false;

        document.getElementById( this.aDivs[this.nAct] ).style.visibility = "visible";

        this.nInt = 50;
        this.nTime = 0;

        var p=this;
        this.nID2 = setInterval(function() { p._fade() }, this.nInt);
    }

    _bsn.Crossfader.prototype._fade = function()
    {
        this.nTime += this.nInt;

        var ieop = Math.round( this._easeInOut(this.nTime, 0, 1, this.nDur) * 100 );
        var op = ieop / 100;
        document.getElementById( this.aDivs[this.nAct] ).style.opacity = op;
        document.getElementById( this.aDivs[this.nAct] ).style.filter = "alpha(opacity="+ieop+")";

        if (this.nOldAct > -1)
        {
            document.getElementById( this.aDivs[this.nOldAct] ).style.opacity = 1 - op;
            document.getElementById( this.aDivs[this.nOldAct] ).style.filter = "alpha(opacity="+(100 - ieop)+")";
        }

        if (this.nTime == this.nDur)
        {
            clearInterval( this.nID2 );

            if (this.nOldAct > -1)
                document.getElementById( this.aDivs[this.nOldAct] ).style.visibility = "hidden";    

            var p=this;
            this.nID1 = setInterval(function() { p._newfade() }, this.nDelay);
        }
    }

    _bsn.Crossfader.prototype._easeInOut = function(t,b,c,d)
    {
        return c/2 * (1 - Math.cos(Math.PI*t/d)) + b;
    }
}

关于JavaScript 不会执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14184472/

相关文章:

Javascript:匹配使用 dom 加载页面

javascript - 使用 Sanctuary 从具有特定 RecordType 的对象中选取字段

javascript - React Native 中的导航

html - 将 height 100% 设置为 html 和 body

javascript | Android 的点击 + 鼠标移动

html - 忽略父填充

javascript - 在 javascript 脚本 block 中评估 Razor @Request.IsLocal

javascript - javascript 中的执行顺序问题

html - 受 Staticrypt 保护的 Jekyll 网站,将 encrypted.html 放在哪里?

html - 图像和 div 内联对齐