1

我将此 JS 页面链接到 XML 所有元素都是视图,但 xml 文件中的音频标签不是 html5 页面中的视图。任何建议如何使此函数检索音频文件并将其显示为 html5 音频播放器。

init: function () {

    //jQuery ajax call to retrieve the XML file
    $.ajax({
        type: "GET",
        url: XMLLIST.xml,
        dataType: "xml",            
        success: XMLLIST.parseXML
    }); 

}, // end: init()

parseXML: function (xml) {

    //Grab every single ITEM tags in the XML file
    var data = $('item', xml).get();
    //Allow user to toggle display randomly or vice versa
    var list = (XMLLIST.random) ? XMLLIST.randomize(data) : data;
    var i = 1;

    //Loop through all the ITEMs
    $(list).each(function () {

        //Parse data and embed it with HTML
        XMLLIST.insertHTML($(this));            

        //If it reached user predefined total of display item, stop the loop, job done.
        if (i == XMLLIST.display) return false;
        i++;
    });


}, // end: parseXML()

insertHTML: function (item) {

    //retrieve each of the data field from ITEM
    var url = item.find('url').text();
    var image = item.find('image').text();
    var audio=item.find('audio').text();
    var title = item.find('title').text();
    var desc = item.find('desc').text();
    var html;

    //Embed them into HTML code
    html = '<div class="item">';
    html += '<a href="' + url + '"><image"' + image + '" alt="' + title + '" />';

    html += '<span>' + title + '</span></a>';
    html += '<audio control="control"><span> ' +audio+' </span></audio>';
    html += '<p>' + desc + '</p>';
    html += '</div>';

    //Append it to user predefined element
    $(html).appendTo(XMLLIST.appendTo);

}, // end: insertHTML()


randomize: function(arr) {

    //randomize the data
    //Credit to JSFromHell http://jsfromhell.com/array/shuffle
    for(var j, x, i = arr.length; i; j = parseInt(Math.random() * i), x = arr[--i], arr[i] = arr[j], arr[j] = x);
        return arr;

} // end: randomize()    

}

4

1 回答 1

0

AFAIK.each()用于迭代一个 jQuery 对象,如果你想迭代一个数组使用$.each()

在这里你有

$(list).each(function () {

应该是

$.each(list, function () {

因为 list 是一个 dom 节点数组,而不是一个 jQuery 对象

于 2012-06-16T00:01:47.417 回答