我想知道如何替换函数中的变量并用另一个函数调用该函数。我正在做一个显示视频的youtube Feed。我想在用户点击时加载一个功能。我已经调用了将显示播放列表的功能,但是当我点击导航链接时我想显示另一个播放列表。我想更改加载特定Feed的变量。任何帮助将非常感激。感谢
<script type="text/javascript">
var feedUrl = "";
function loadFeedAPI() {
google.load("feeds", "1");
google.setOnLoadCallback(initialize);
}
function playVideo() {
//Examples of how to assign the Colorbox event to elements
$(".youtubeThumb").colorbox({ iframe: true, innerWidth: 640, innerHeight: 390 });
};
function initialize(feedUrl) {
$(".youtubeFeed").empty();
feedUrl = "http://gdata.youtube.com/feeds/api/playlists/PLlVlyGVtvuVlzDWbR0rXf1YnpJ9JL74FV";
new google.feeds.lookupFeed(feedUrl, function (result) {
if (result.error || !result.url) {
$('#videocomm').hide();
return;
}
// get the feed items
var feed = new google.feeds.Feed(result.url);
feed.setNumEntries(25);
feed.load(function (result) {
// write out the feed data
var container = $(".youtubeFeed");
var totalcount = result.feed.entries.length;
for (var i = 0; i < result.feed.entries.length; i++) {
var entry = result.feed.entries[i];
var vidhash = /=(.*)&(.*)$/.exec(entry.link)[1];
var videoParmeters = "?rel=0&autohide=1&showinfo=0&autoplay=1&modestbranding=1&?vq=hd1080;wmode=transparent";
var VidTitleMaxLength = 42;
var countVidTitleLength = entry.title.valueOf().length;
var joinName = entry.title.split(/[ ,]+/).join('');
var joinNameLength = joinName.valueOf().length;
var strtemp = entry.title.substr(0, VidTitleMaxLength) + "...";
console.log(strtemp + joinNameLength);
container.append('<li><div><a class="youtubeThumb" href="http://www.youtube.com/embed/' + vidhash + videoParmeters + '" title="' + entry.title + '"><img src="https://img.youtube.com/vi/' + vidhash + '/2.jpg" /><br />' + '<p class="videoTitle">' + strtemp + '</p>' + '</a></div></li>\n');
}
playVideo();
});
});
}
function loadTest(feedUrl) {
feedUrl.initialize("http://gdata.youtube.com/feeds/api/playlists/PLlVlyGVtvuVnAg43cejh4HGzap_MPRNhV");
alert("hi");
return false;
}
loadFeedAPI();
</script>
<div id="navigation">
<ul id="tsSideNavUL">
<li><a href="javascript:initialize();">The Roots of Our Vine</a></li>
<li><a href="javascript:loadTest();">A Soiree From Start to Finish</a></li>
<li><a href="#">Booking</a></li>
<li><a href="#">Selling</a></li>
<li><a href="#">Enrolling</a></li>
</ul>
</div>
答案 0 :(得分:0)
你的问题是关于javascript的核心概念,你可能应该更深入地阅读它。也就是说,您无法从一个函数内部访问另一个函数中的变量,因为函数会创建一个封闭范围。你能做的就是把所有的功能都放在一个对象上。
var youtube = {
feedUrl: "http://gdata.youtube.com/feeds/api/playlists/PLlVlyGVtvuVlzDWbR0rXf1YnpJ9JL74FV",
loadFeedApi: function() {
google.load("feeds", "1");
google.setOnLoadCallback(initialize);
},
playVideo: function() {
//Examples of how to assign the Colorbox event to elements
$(".youtubeThumb").colorbox({ iframe: true, innerWidth: 640, innerHeight: 390 });
},
initialize: function (url) {
$(".youtubeFeed").empty();
var feedUrl = url || this.feedUrl;
new google.feeds.lookupFeed(feedUrl, function (result) {
if (result.error || !result.url) {
$('#videocomm').hide();
return;
}
// get the feed items
var feed = new google.feeds.Feed(result.url);
feed.setNumEntries(25);
feed.load(function (result) {
// write out the feed data
var container = $(".youtubeFeed");
var totalcount = result.feed.entries.length;
for (var i = 0; i < result.feed.entries.length; i++) {
var entry = result.feed.entries[i];
var vidhash = /=(.*)&(.*)$/.exec(entry.link)[1];
var videoParmeters = "?rel=0&autohide=1&showinfo=0&autoplay=1&modestbranding=1&?vq=hd1080;wmode=transparent";
var VidTitleMaxLength = 42;
var countVidTitleLength = entry.title.valueOf().length;
var joinName = entry.title.split(/[ ,]+/).join('');
var joinNameLength = joinName.valueOf().length;
var strtemp = entry.title.substr(0, VidTitleMaxLength) + "...";
console.log(strtemp + joinNameLength);
container.append('<li><div><a class="youtubeThumb" href="http://www.youtube.com/embed/' + vidhash + videoParmeters + '" title="' + entry.title + '"><img src="https://img.youtube.com/vi/' + vidhash + '/2.jpg" /><br />' + '<p class="videoTitle">' + strtemp + '</p>' + '</a></div></li>\n');
}
playVideo();
});
});
},
loadTestUrl: function(url) {
this.initialize(
"http://gdata.youtube.com/feeds/api/playlists/PLlVlyGVtvuVnAg43cejh4HGzap_MPRNhV"
);
alert("hi");
return false;
}
}
然后是你的标记
<div id="navigation">
<ul id="tsSideNavUL">
<li><a href="javascript:youtube.initialize();">The Roots of Our Vine</a></li>
<li><a href="javascript:youtube.loadTest();">A Soiree From Start to Finish</a></li>
<li><a href="#">Booking</a></li>
<li><a href="#">Selling</a></li>
<li><a href="#">Enrolling</a></li>
</ul>
</div>
注意&#39; youtube&#39;在函数调用中。