繁体   English   中英

Javascript-仅在两次单击按钮后显示页面

[英]Javascript - Page displays only after button clicked twice

第一个问题是,第一次单击该按钮时无法显示该页面。 第二个相关问题是,第一次单击按钮时出现错误“无法读取未定义的属性'innerHTML'的定义”,尽管单击按钮两次后一切都很好。

main.js

var surveyPage = '';
var surveyQuestions = '';
var surveyPageData = '';

var fakeDiv = document.createElement( 'div' );

function get_surveyPageHtml(url) {
    $.get(url, function(data) {
        //console.log(data);
        surveyPage = data;

        fakeDiv.innerHTML = '';
        fakeDiv.innerHTML = data;

    });
    surveyQuestions = '';
    surveyQuestions = fakeDiv.getElementsByClassName('question');
    console.log(surveyQuestions);

    surveyPageData = surveyQuestions[1].innerHTML;
    console.log(surveyPageData);
}

$(document).ready(function() {

    url = "page/page.html";
    $("#button").click(function(){   

        get_surveyPageHtml(url);
        $("#display").html(surveyPage); 

    });
});

的index.html

<!DOCTYPE html>
<html>
<head>
    <title>TEST</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

    <script type="text/javascript" src="jquery-1.11.2.min.js"></script>
    <script type="text/javascript" src="main.js"></script>

</head>
<body>
    <div>
        <button id="button">Get page data</button>
    </div>

    <div id="display"></div>

</body> 
</html>

页/ page.html中

<h2>Survey</h2>

<label class="question">Question 1</label>
<label class="question">Question 2</label>
<div class="question">
    Question 3
    <label class="question">Question 4</label>
</div>

您问题的元凶是:

get_surveyPageHtml(url);
$("#display").html(surveyPage); 

AJAX请求需要一些时间,因此您尝试在AJAX请求完成之前使用响应。 您可以通过移动$("#display").html(surveyPage);来解决此问题$("#display").html(surveyPage); 这里:

//...
$.get(url, function(data) {
    //console.log(data);
    surveyPage = data;

    fakeDiv.innerHTML = '';
    fakeDiv.innerHTML = data;

    $("#display").html(surveyPage);
})

这样,当AJAX响应进入时将加载页面。

- 编辑 -

Cannot read property 'innerHTML' of undefined原因与第一个相同,只是在不同的代码块中。 这是你应该这样做的:

function get_surveyPageHtml(url) {
    $.get(url, function(data) {
        //console.log(data);
        surveyPage = data;

        fakeDiv.innerHTML = '';
        fakeDiv.innerHTML = data;
        $("#display").html(surveyPage);

        surveyQuestions = '';
        surveyQuestions = fakeDiv.getElementsByClassName('question');
        console.log(surveyQuestions);

        surveyPageData = surveyQuestions[1].innerHTML;
        console.log(surveyPageData);

    });

}

为了澄清一些问题,在ajax完成之后,您的其他语句也应该执行。

$.get(url, function(data) {

    // code...

    surveyQuestions = '';
    surveyQuestions = fakeDiv.getElementsByClassName('question');
    console.log(surveyQuestions);
    surveyPageData = surveyQuestions[1].innerHTML;
    console.log(surveyPageData);
}

如果您使用的是jQuery,这是您的最佳做法,我建议您这样做:

http://plnkr.co/edit/Rr5UtYcHNTUf0cEMf9pc (按运行并查看main.js)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM