0

我有这个使用哈佛 API 的代码,它可以将所有的 CS 课程加载到下拉列表中。选择一门课程后,我想绘制一张包含课程所有信息的表格,但由于某种原因,我让它工作,该功能没有触发(我认为这是问题所在)。有人可以看到我写的函数中的问题吗?

<script type="text/javascript">
         function loadXMLDoc() {
             document.getElementById("span").style.visibility = "visible";
             document.getElementById("button").style.visibility = "hidden";
             $.ajax({
                 type: "GET",
                 url: "http://courses.cs50.net/api/1.0/courses?field=COMPSCI&output=xml",
                 success: function (data) {
                     var courses = data.documentElement.getElementsByTagName("course");
                     var options = document.createElement("select");
                     $(options).change(function () {
                         ShowCourseDetails(this);
                     });
                     for (var i = 0; i < courses.length; i++) {
                         var option = document.createElement("option");
                         option.value = $(courses[i]).find("cat_num").text();
                         option.text = $(courses[i]).find("title").text();
                         options.add(option, null);
                     }
                     document.getElementById("selectDiv").appendChild(options);
                     document.getElementById("span").style.visibility = "hidden";
                 }
             });
         }
         //Get the Course information
         function ShowCourseDetails(event) {
             // get the index of the selected option 
             var idx = event.selectedIndex;
             // get the value of the selected option 
             var cat_num = event.options[idx].value;
             $.ajax({
                 type: "GET",
                 url: "http://courses.cs50.net/api/1.0/courses?output=xml&&cat_num=" + cat_num,
                 success: function (data) {
                     $("#TableDiv").html(ConvertToTable(data.documentElement));
                 }
             });
         }
         //Draw The Table
         function ConvertToTable(targetNode) {
             targetNode = targetNode.childNodes[0];
             // first we need to create headers
             var columnCount = 2;
             var rowCount = targetNode.childNodes.length
             // name for the table
             var myTable = document.createElement("table");
             for (var i = 0; i < rowCount; i++) {
                 var newRow = myTable.insertRow();
                 var firstCell = newRow.insertCell();
                 firstCell.innerHTML = targetNode.childNodes[i].nodeName;
                 var secondCell = newRow.insertCell();
                 secondCell.innerHTML = targetNode.childNodes[i].text;
             }
             // i prefer to send it as string instead of a table object
             return myTable.outerHTML;
         }
</script>

标记:

<div class="left">
                   <input id="button" type="button" onclick="loadXMLDoc()" value="Get all Coputer Science Courses From Harvard"/>
                   <br />
                   <span id="span" style="visibility: hidden">Downloading Courses From Harvard.. Please Wait.. </span>
                   <div id="selectDiv"></div>
                   <div id="TableDiv"></div>
            </div>

提前10倍。

4

1 回答 1

0

为了让它调用,ShowCourseDetails我更改了此代码:

$(options).change(function () {
    ShowCourseDetails(this);
});

对此:

$('select').change(function () {
    ShowCourseDetails(this);
});

并将其移至回调函数的底部,如下所示:

document.getElementById("span").style.visibility = "hidden";

以便在添加事件后将事件连接到 DOM 元素。然后我不得不更改此代码:

$("#TableDiv").html(ConvertToTable(data.documentElement));

对此,由于 documentElement 似乎没有被定义:

$("#TableDiv").html(ConvertToTable(data.childNodes[0]));

然后允许我在你的ConvertToTable函数中删除这一行:

targetNode = targetNode.childNodes[0];

它仍然无法正常工作,因为您没有正确浏览返回的 XML。但我会留给你解决这个问题。

于 2012-07-22T17:49:17.883 回答