【问题标题】:YouTube ajax, .replace() not workingYouTube ajax,.replace() 不起作用
【发布时间】:2012-05-15 04:39:52
【问题描述】:

我正在使用下面的 jquery,并且在 var video_tags=data.tags 中添加 .replace(",", " ") 不会返回值。如何用空格替换逗号。

http://jsfiddle.net/hJGe4/1/

$(document).ready(function () {
    $(".search_input").focus();
    $(".search_input").keyup(function () {
        $("#video").html('');
        var search_input = $(this).val();
        var keyword = encodeURIComponent(search_input);
        var yt_url = 'http://gdata.youtube.com/feeds/api/videos?q=' + keyword + '&format=5&max-results=6&v=2&alt=jsonc';
        $.ajax({
            type : "GET",
            url : yt_url,
            dataType : "jsonp",
            success : function (response) {
                if (response.data.items) {
                    $.each(response.data.items, function (i, data) {
                        var video_title = data.title;
                        var video_tags = data.tags.replace(",", " ");
                        var final = video_title + '<br/>' + video_tags + '<br/><br/><br/>';
                        $("#video").append(final);
                    });
                } else {
                    $("#video").html("<div id='no'>No Video</div>");
                }
            }
        });
    });
});

【问题讨论】:

    标签: javascript jquery ajax replace jsonp


    【解决方案1】:

    var video_tags=data.tags.replace(",", " ");

    应该是

    var video_tags=data.tags.join(" ");

    DEMO

    【讨论】:

      猜你喜欢
      • 2013-11-03
      • 1970-01-01
      • 2013-01-09
      • 2014-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-04
      • 2019-03-29
      相关资源
      最近更新 更多