簡體   English   中英

jQuery文檔就緒功能太長,我想導出其中的一部分

[英]jquery document ready function too long, I want to export parts of it

好的,盡管我已經使用了很長時間,但這是我第一次問這個問題。 我似乎找不到我想要的東西。

我有一個文檔就緒功能,其中的一些部分希望我可以“導出”,因此不需要在每一頁上都重復這些內容。 有點像ASP,包括頁眉,頁腳甚至是主容器。 這可行嗎? 請看一下我的意思:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<title>Some Title</title>
<link rel="styleSheet" href="somePath/myCssFile.css" type="text/css" media="screen" />
<script src="../somePath/jquery.min.js" type="text/javascript"></script>
<script src="../somePath/my_own_JS_stuff.js" type="text/javascript"></script>
<script>
$(document).ready(function () {
    // this part is unique to THIS page

    $('#myUniqueDiv').delay(1000).fadeIn(500);
    $(document).attr("title", "MisterKay.com | " + someArray[parseInt(a) + parseInt(b)]);
    $('#myUniqueDiv').hover(function(){ 
        // some code here
    }, function(){
        // some more code here
    });

    // This part is repeated on all pages of the site.
    // I wish I could take this part out of here, put it in a file and just call the file, like a virtual file in ASP.
    // That way, if I make a modification, I could do it once for the entire site, instead for doing it for each page.
    // I cannot put it in a separate JS file either; for some reason it gives me an error.
    // My guess is this happens because this is INSIDE of the document-ready-function.
    // But I also cannot put it outside of the document-ready-function. 
    $('#someRepeatedDiv').cycle({ 
        fx:     'fade', 
        speed:  1000, 
        timeout:    3000
    });
    $('#someRepeatedInputBox').blur(function(){
        var sVal = $(this).val();
        if(sVal == ""){
            $(this).val(someArray[a]);
            $(this).css('color','#999');
        }
    });
    // End of repeated section of the document-ready-function
}); // End of document-ready-function
</script>
</head>
<body>
        <!-- #include virtual="header.html" -->
        <!-- #include virtual="main.html" -->
        <!-- #include virtual="footer.html" -->
</body>
</html>

如何將需要在每頁上重復的部分放入某個外部文件,並仍由文檔就緒功能讀取?

再次感謝您的解答!

您可以嘗試jQuery $ .getScript():

Load a JavaScript file from the server using a GET HTTP request, then execute it.

例:

$.getScript( "ajax/test.js", function( data, textStatus, jqxhr ) {
  console.log( data ); // Data returned
  console.log( textStatus ); // Success
  console.log( jqxhr.status ); // 200
  console.log( "Load was performed." );
});

有關更多詳細信息: http : //api.jquery.com/jquery.getscript/

暫無
暫無

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

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