簡體   English   中英

通過AJAX打開頁面時,jQuery功能不起作用。

[英]Jquery functionality not working when opening the page via AJAX.

我正在嘗試通過AJAX打開一個具有jquery功能的HTML頁面。

我要打開的頁面是:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" 
    "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Slick Slide Up and Down Thumbnail Effect with jQuery</title>
<script type="text/javascript" src="../scripts/jquery-1.3.1.min.js"></script>
<script type="text/javascript" src="../scripts/jquery.easing.1.3.js"></script>
<script>
    $(document).ready(function () {

        // transition effect
        style = 'easeOutQuart';

        // if the mouse hover the image
        $('.photo').hover(
            function() {
                //display heading and caption
                $(this).children('div:first').stop(false,true).animate({top:0},{duration:200, easing: style});
                $(this).children('div:last').stop(false,true).animate({bottom:0},{duration:200, easing: style});
            },

            function() {
                //hide heading and caption
                $(this).children('div:first').stop(false,true).animate({top:-50},{duration:200, easing: style});
                $(this).children('div:last').stop(false,true).animate({bottom:-50},{duration:200, easing: style});
            }
        );

    });
    </script>
<style>.photo{position:relative;font-family:arial;overflow:hidden;border:5px solid #000;width:350px;height:233px;}.photo .heading,.photo .caption{position:absolute;background:#000;height:50px;width:350px;opacity:0.6;}.photo .heading{top:-50px;}.photo .caption{bottom:-50px;left:0px;}.photo .heading span{color:#26c3e5;top:-50px;font-weight:bold;display:block;padding:5px 0 0 10px;}.photo .caption span{color:#999;font-size:9px;display:block;padding:5px 10px 0 10px;}</style>
</head>
<body>
<div class="photo">
<div class="heading"><span>Telephoto Lens</span></div>
<img src="images/fall.jpg" width="350" height="233" alt=""/>

</body>
</html>

通過ajax打開此頁面的index.html頁面具有以下代碼:

<script language="JavaScript" type="text/javascript">

        //Gets the browser specific XmlHttpRequest Object
        function getXmlHttpRequestObject() {
            if (window.XMLHttpRequest) {
                return new XMLHttpRequest(); //Not IE
            } else if(window.ActiveXObject) {
                return new ActiveXObject("Microsoft.XMLHTTP"); //IE
            } else {
                //Display your error message here. 
                alert("Your browser doesn't support the XmlHttpRequest object.  Better upgrade.");
            }
        }           
        //Get our browser specific XmlHttpRequest object.
        var receiveReq = getXmlHttpRequestObject();     
        //Initiate the asyncronous request.

        function init(){
            sayHello(1); 
        }

        window.onload=init;

        function sayHello(x) {          
            //If our XmlHttpRequest object is not in the middle of a request, start the new asyncronous call.
            if (receiveReq.readyState == 4 || receiveReq.readyState == 0) {
receiveReq.open("GET", 'pages/mobile.html', true);
//Set the function that will be called when the XmlHttpRequest objects state changes.
                receiveReq.onreadystatechange = handleSayHello; 
                //Make the actual request.
                receiveReq.send(null);
            }           
        }
        //Called every time our XmlHttpRequest objects state changes.
        function handleSayHello() {
            //Check to see if the XmlHttpRequests state is finished.
            if (receiveReq.readyState == 4) {
                document.getElementById('span_result').innerHTML = receiveReq.responseText;
}
        }

HTML部分供您查看:

<div id="nav">
                    <table class="nav">
                      <tr><th>&nbsp</th></tr>
                      <tr><td id="selected"><a href="javascript:sayHello(1);">Distinguished Techonologist Program</a></td></tr>
                      <tr><td><a href="javascript:sayHello(2);">Mobile Solution</a></</td></tr>
                      <tr><td><a href="javascript:sayHello(3);">HTML5 Canvas</a></td></tr>
                      <tr><td><a href="javascript:sayHello(4);">Doamin Expertise</a></td></tr>    
                    </table>
                </div>

<div id="carousel">
                    <span id="span_result"></span>
                </div>

請幫助。

提前致謝

問候Zeeshan

innerHTML引入的腳本將不會執行。 您應該使用DOM方法createElement,appendChild來構建頁面。 一個簡單的例子:

script = document.createTextNode("alert('Run')");
tag =  document.createElement('script');
tag.appendChild(script);
div.appendChild(tag);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM