使用 $.get,需要在隐藏页面上初始化变量

Using $.get, need to initialize variables on a hidden page

本文关键字:初始化 变量 隐藏 get 使用      更新时间:2023-09-26

我有以下代码行来调出一个页面,该页面包含多个页面,可以通过单击选项卡来显示:

<script type="text/javascript">
$(document).ready(function() {
    //Default Action
    $(".tab_content").hide(); //Hide all content
    $("ul.tabs li:first").addClass("active").show(); //Activate first tab
    $(".tab_content:first").show(); //Show first tab content
    //On Click Event
    $("ul.tabs li").click(function() {
        $("ul.tabs li").removeClass("active"); //Remove any "active" class
        $(this).addClass("active"); //Add "active" class to selected tab
        $(".tab_content").hide(); //Hide all tab content
        var activeTab = $(this).find("a").attr("href"); //Find the rel attribute value to identify the active tab + content
        $(activeTab).fadeIn(); //Fade in the active content
        return false;
    });
});
</script>
...
<div id="tabBarArea">
    <div id="tabBarContainer">
        <ul class="tabs">
            <li id="home"><a href="#home">Home</a></li> -->
            <li id="readingListTab"><a href="#books">Commander's Reading List</a></li>
            <li id="moviesTab"><a href="#movies">Lecture Series</a></li>
        </ul>
    </div>
</div>
<div class="container">
    <div id="tab_container">
        <div id="home" class="tab_content">
            <div id="homeHeaderGraphic" class="header">
                <img id="homeHeaderGraphicImage" src="images/CommandProfessionalDeveloment%20ResourceGuide%20Header-Title.png">
            </div>
            <div id="homeContent" class="contentArea">
                <script type="text/javascript">
                    $.get("Home.html", function(data){
                        $("#homeContent").empty().append(data);
                     }, "html");
                    resizeContentAreaHeight("home", "homeHeaderGraphic", "homeContent");
                </script>
            </div>
        </div>
        <div id="books" class="tab_content">
            <div id="booksHeaderGraphic" class="header">
                <img id="booksHeaderGraphicImage" src="images/Commander%27sReadingList%20Header.png">
            </div>
            <div id="booksContent" class="contentArea">
                <script type="text/javascript">
                    $.get("CommanderReadingList.html", function(data){
                        $("#booksContent").empty().append(data);
                     }, "html");
                    resizeContentAreaHeight("books", "booksHeaderGraphic", "booksContent");
                </script>
            </div>
        </div>
        <div id="movies" class="tab_content">
            <div id="moviesHeaderGraphic" class="header">
                <img id="moviesHeaderGraphicImage" src="images/LectureSeries%20Logo.png">
            </div>
            <div id="moviesContent" class="contentArea">
                <script type="text/javascript">
                    $.get("ForumsArchives.htm", function(data, status, xhr){
                        $("#moviesContent").empty().append(data);
                     }, "html");
                    resizeContentAreaHeight("movies", "moviesHeaderGraphic", "moviesContent");
                </script>
            </div>
        </div>
    </div>
</div>

我遇到的问题是,在调出阅读列表时,它需要初始化页面上的一些变量,即使它是隐藏的。 变量的值应如下所示:

trueLittleBooksResponsiveOffsetTop: 363 
$('#readingListIntro').offset().top: 263 
$('#readingListIntro').css('margin-top'): 0 
$('#readingListIntro').height(): 100 
littleBooksResponsiveTop: 365 
littleBooksResponsiveHeight: 0 
bookMenuBarHeightToMove: 161 
readingListIntroTop: 263 
readingListIntroHeight: 100

但是当"主页"选项卡是第一个出现的时,它就会出现这样。

trueLittleBooksResponsiveOffsetTop: 0
$('#readingListIntro').offset().top: 0
$('#readingListIntro').css('margin-top'): 0px
$('#readingListIntro').height(): 0
littleBooksResponsiveTop: 0
littleBooksResponsiveHeight: 0
bookMenuBarHeightToMove: -204
readingListIntroTop: 0
readingListIntroHeight: 0

据我所知,问题是第二个选项卡在页面启动期间不可见。 当我使书籍选项卡成为第一个可见时,它会正确初始化,但是当我使用主页作为第一个选项卡时,它给了我错误的变量。 我需要获取不可见页面的变量以进行初始化。

您可以使用以下

CSS 集中的任何一个来使页面可见并计算内容并将其还原:

position: absolute;
z-index: 1;
top: -200%;

或者别的什么:

display: block;
position: absolute;
z-index: 1;
visibility: hidden;

上面的 CSS 必须在 AJAX 事件的success函数中设置。

我删除了一些加载到div 中的页面的动态,并对图画书菜单栏的高度进行了硬编码,以便图画书菜单栏始终保持不变。 然后不必进行初始计算。 我想,只要变量不能改变(太多),计算的需要就消失了。 不是最好的,但我处于有限的环境中,所以用户应该不需要太大的差异。