1

Goal:

When click_div is clicked, the script should read a file (text.html) from my own computer using $.get() method, and set the html of a div (cont_div) to the file's content.

Code:

$(document).ready(function() {
    $('#click_div').click(function(){
        var HTML_FILE_URL = 'text.html';
        $.get(HTML_FILE_URL, function(data){
            alert(data);
            $('#cont_div').html(text);
        });
    });
});

Problem:

The content of cont_div keeps blank, after clicking click_div. I put an alert to show the content of what the get method read, and it displays a blank dialog.

Further information:

• The file is on the same folder as the .js file, and the index.html page file.

• All the other javascript functions work well.

Question:

Given that the alert function displays nothing, but is still called, what could I possibly be doing wrong? I've tried many things, but it really doesn't work.

4

1 回答 1

0

看起来您附加了错误的变量

$('#cont_div').html(text);

应该是

$('#cont_div').html(data);

另外,我会调查$.load(). 我认为你的情况会更简单一些。

就像是:

$(document).ready(function() {
    $('#click_div').click(function(){
        var HTML_FILE_URL = 'text.html';
         $('#cont_div').load(HTML_FILE_URL);
    });
});

应该管用。

于 2013-08-28T18:11:40.350 回答