0

我有 HTML 代码:

<div id=ytVid>
    <iframe width="420" height="315" src="http://www.youtube.com/embed/5EdmHSTwmWY" frameborder="0" allowfullscreen></iframe>
    <script type="text/javascript" src="js/youtube.js"></script>
</div>

目前,我有一组由 jQuery 使用数组生成的段落。

我想要发生的是,当用户单击生成的段落之一时,这将从 youtube.js 文件启动此代码:

$(document).ready(function() {
    function openLink(evt) {
        var search = evt.target.innerHTML;
        var keyword= encodeURIComponent(search);
        var yt_url='http://gdata.youtube.com/feeds/api/videos?q='+keyword+'&format=5&max-results=1&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_id=data.id;
                        var video_frame="<iframe width='420' height='236' src='http://www.youtube.com/embed/"+video_id+"' frameborder='0' type='text/html'></iframe>";
                        $("#ytVid").html(video_frame); 
                    });
                } else {
                    $("#ytVid").html("<div id='no'>No Video</div>");
                }
            }
        });
    };
});

我不知道上面的代码是否正确,但它应该做的是使用数组中的内容(如果可能)或段落中的内容来搜索 youtube 的视频以更新 ytVid div。

谢谢

4

1 回答 1

0

In your "simplified" copy of the JavaScript jsfiddle, you have the strings wrapped in brackets, which signifies they are arrays. You also didn't have jQuery selected to be included.

http://jsfiddle.net/PADL9/1/

And here's one with the JS from above added in

http://jsfiddle.net/PADL9/2/

(remove the alert/return for the AJAX to work)

于 2012-04-26T03:15:23.923 回答