0

これが私の関数です:

  $().ready(function() {
    console.log('DOM is ready');
    var songtoload = $(".soundcloudid").first().html();
    console.log(songtoload)
    if (songtoload == undefined) {
      console.log('Hide the widget');
      $("#sc-widget").hide();
    }
    if (songtoload !== undefined) {
      console.log('Show the widget');
      $("#sc-widget").show();
    }
  });

何らかの理由で、Chromeコンソールで次の応答が表示されても、songtoloadは「未定義」として表示され続けます。

$(".soundcloudid").first().html();
"31204641"

.readyを間違って使用していますか?

編集 これの目的は、曲がプレイリストの部分に存在する場合にのみSCプレーヤーウィジェットを表示することです

4

2 に答える 2

1

songcloudidオブジェクトは動的にロードされるため、ウィジェットを切り替えるチェックは、これらのオブジェクトを動的にロードする関数に移動する必要があります。

function refreshPlaylist() {
    // ... some code to refresh it
    $.ajax({
        // your ajax call to refresh it
        success: function (data) {
            // ... load your data first and put it on the page
            var $songToLoad = $('.soundcloudid:first');
            if ($songToLoad.length == 1) {
                console.log($songToLoad.html())
                console.log('Show the widget');
                $("#sc-widget").show();
            }
            else {
                console.log('Hide the widget');
                $("#sc-widget").hide();
            }
        }
    });

}
于 2013-03-01T18:13:04.677 に答える
0

$().ready$()結果の準備ができるまで待機します。$()空のセットです。私があなたが望むと思うのは、$(document).ready(function() {...})またはその近道です:$(function() {...})

于 2013-03-01T19:19:35.560 に答える