如何将css添加到json响应中

时间:2012-02-24 20:58:04

标签: javascript json

点击td class =“bgimg”,我正在调用另一个函数。如何在我点击的td中添加一个类?

/*This function creates a list of tabs*/
BCL.onSearchResponse = function(jsonData) {
    BCL.jsonData = jsonData;
    var str = "<table id=\"playlistTable\" cellspacing=\"1\"><tbody><tr>";
    var html = "";
    for (var i = 0; i < jsonData["items"].length; i++) {
        var playlist = jsonData["items"][i];
        html = "<td class=\"bgimg\" onclick=\"BCL.onPlaylistSelect(" + i +")\">{{name}}</td>";
        str += BCL.markup(html,playlist);
    }
    str += "</tr></tbody></table>";
    //console.log(str);
    document.getElementById("results").innerHTML = str;
    // load the first playlist
    BCL.onPlaylistSelect(0);
}

1 个答案:

答案 0 :(得分:1)

function hasClass(element,clss) {
    return element.className.match(new RegExp('(\\s|^)'+clss+'(\\s|$)'));
}

function addClass(element,clss) {
    if (!this.hasClass(element,clss)) element.className += " "+clss;
}

BCL.onPlaylistSelect = function(something, element) {
    element.addClass("myClass");
    //do stuff
};

BCL.onSearchResponse = function(jsonData) {
    BCL.jsonData = jsonData;
    var str = "<table id=\"playlistTable\" cellspacing=\"1\"><tbody><tr>";
    var html = "";
    for (var i = 0; i < jsonData["items"].length; i++) {
        var playlist = jsonData["items"][i];
        html = "<td class=\"bgimg\" onclick=\"BCL.onPlaylistSelect(" + i +", this)\">{{name}}</td>";
        str += BCL.markup(html,playlist);
    }
    str += "</tr></tbody></table>";
    //console.log(str);
    document.getElementById("results").innerHTML = str;
    // load the first playlist
    BCL.onPlaylistSelect(0);
};