Javascript - 为什么<h1>标签的innerHTML不会改变?

时间:2015-04-24 12:03:14

标签: javascript html rest

我正在尝试通过以下代码中的Javascript更改我的标签的innerHTML:

window.onload = function() {

    var doSomethingAJAX = function (el, url) {
    var xhr = new XMLHttpRequest();
    xhr.open("GET", url, true);
    xhr.onload = function () { 

        var data = JSON.parse(xhr.responseText);
        var tabs = document.getElementsByClassName("tab");
        var tab = tabs[0];
        var taskName = data.data[0].name;
        alert(tab + " - " + taskName);
        var taskId = data.data[0].id;
        tab.innerHTML = taskName;
        getFormProperties(taskId);

    };
    xhr.onerror = function () { alert("error") };
    xhr.send();
};

code continues.....

警报正确显示[object HTMLHeadingElement]和任务名称,但它不会以应有的方式更改innerHTML。我错过了一些明显的东西吗?

HTML:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<script type="text/javascript" src="js/apitest.js"></script>
<style type="text/css" media="screen">@import "css/tabs.css";</style>
<title>Tabs and REST</title>
</head>
<body>
    <div id='tab-container'>
        <div class="tab-content">
                <h1 class="tab" title="title for page 1">Page 1</h1>
                <form id="form" action="CompleteTask" method="post">
                </form>
        </div>
        <div class="tab-content">
                <h1 class="tab" title="title for page 2">Page 2</h1>
                <p>This is the content of tab 2 on container 1</p>
        </div>
        <div class="tab-content">
                <h1 class="tab" title="title for page 3">Page 3</h1>
                <p>This is the content of tab 3 on container 1</p>
        </div>
    </div>
    <script type="text/javascript" src="js/tabs.js"></script>
</body>
</html>

0 个答案:

没有答案