两个功能不能协同工作,而是单独工作

时间:2014-05-26 12:37:28

标签: javascript jquery html css

我使用Spotify脚本将现在播放的歌曲从html放入<div>,他们自己工作,但当我把它们放在同一页面上时,只有其中一个显示:< / p>

HTML:

<div id="rss"></div>
<div id="lukerss"></div>

JavaScript的:

(function () {
    var e = "LukeAssassin";
    window.lfmRecentTrack = function(t) {
        var n = (new Array).concat(t.recenttracks.track)[0];
        var album, spurl;
        if (n.album["#text"]) {
            album = " (from " + n.album["#text"] + ")";
        } else {
            album = "";
        }
        try {
            var spotify = new XMLHttpRequest();
            spotify.open("GET", "https://ws.spotify.com/search/1/track.json?q=" + encodeURIComponent(n.artist["#text"] + " - " + n.name), false);
            spotify.send();
            var spotresp = JSON.parse(spotify.responseText);
            if (spotresp["tracks"].length > 0) {
                //var urisplit = spotresp["tracks"][0]["href"].split(":");
                //spurl = " https://open.spotify.com/" + urisplit[1] + "/" + urisplit[2];
                spurl = spotresp["tracks"][0]["href"];
            } else {
                console.log("spotify: couldn't get url");
                spurl = "";
            }
        } catch (e) {
            console.log("spotify: " + e.message);
            spurl = "";
        }
        var r2 = "" + n.name + " by " + n.artist["#text"];
        var element = document.getElementById("lukerss");
        element.innerHTML = r2;
    }
    var n = document.createElement("script");
    n.setAttribute("type", "text/javascript");
    n.setAttribute("src", "https://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=" + e + "&api_key=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX&limit=1&format=json&callback=window.lfmRecentTrack");
    document.body.appendChild(n)
})();
(function () {
    var e2 = "DeviousRunner";
    window.lfmRecentTrack = function(t2) {
        var n2 = (new Array).concat(t2.recenttracks.track)[0];
        var album, spurl;
        if (n2.album["#text"]) {
            album = " (from " + n2.album["#text"] + ")";
        } else {
            album = "";
        }
        try {
            var spotify = new XMLHttpRequest();
            spotify.open("GET", "https://ws.spotify.com/search/1/track.json?q=" + encodeURIComponent(n2.artist["#text"] + " - " + n2.name), false);
            spotify.send();
            var spotresp = JSON.parse(spotify.responseText);
            if (spotresp["tracks"].length > 0) {
                //var urisplit = spotresp["tracks"][0]["href"].split(":");
                //spurl = " https://open.spotify.com/" + urisplit[1] + "/" + urisplit[2];
                spurl = spotresp["tracks"][0]["href"];
            } else {
                console.log("spotify: couldn't get url");
                spurl = "";
            }
        } catch (e2) {
            console.log("spotify: " + e.message);
            spurl = "";
        }
        var r = "" + n2.name + " by " + n2.artist["#text"];
        var element = document.getElementById("rss");
        element.innerHTML = r;
    }
    var n2 = document.createElement("script");
    n2.setAttribute("type", "text/javascript");
    n2.setAttribute("src", "https://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=" + e2 + "&api_key=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX&limit=1&format=json&callback=window.lfmRecentTrack");
    document.body.appendChild(n2)
})();

CSS:

#rss {
    width: 500px;
    height: auto;
    float:right;
    text-align:center;
    color:red;
    font-size: 24px;
}
#lukerss {
    width: 500px;
    height: auto;
    float:left;
    color:red;
    font-size:24px;
}

小提琴也是here - 似乎最后一个是显示的那个。如果有人有解决方案,请随时分享:)

1 个答案:

答案 0 :(得分:1)

在两个函数中为window.lfmRecentTrack使用不同的名称。