我正在尝试在最后一个函数中使用TsearchResultID但不知道如何访问它...在此函数中($(document).on('click','。slideYThumbnail',function(event) {)
function QueryGetta0() {
var TTsearchQuery = 'x';
return TTsearchQuery;
}
function QueryGetta1() {
return $.get(
"https://www.googleapis.com/youtube/v3/search",{
part: 'snippet',
maxResults: 1,
q: YTSearchQueryText,
type: 'video',
key: ''}
);
}
$(document).on('click', '.lister ul span', function(event) {
QueryGetta1()
.done(function(data) {
var TsearchResultID = data.items[0].id.videoId;
var target = '<div class="slideYThumbnail"><img class="slideYThumbnailInside" src="http://img.youtube.com/vi/' + TsearchResultID + '/0.jpg"></img></div>';
});
});
$(document).on('click', '.slideYThumbnail', function(event) {
alert(TsearchResultID);
});
答案 0 :(得分:2)
您可以通过在全局范围内创建外部对象来管理它:
var obj = {
TsearchResultID : "" // <---declare it here.
};
function QueryGetta0() {
// other code as is.
}
function QueryGetta1() {
// other code as is.
}
$(document).on('click', '.lister ul span', function(event) {
QueryGetta1()
.done(function(data) {
obj.TsearchResultID = data.items[0].id.videoId; // <---put value here.
var target = '<div class="slideYThumbnail"><img class="slideYThumbnailInside" src="http://img.youtube.com/vi/' + TsearchResultID + '/0.jpg"></img></div>';
});
});
$(document).on('click', '.slideYThumbnail', function(event) {
alert(obj.TsearchResultID); // now access it here.
});
答案 1 :(得分:1)
如果要跨函数共享变量,则必须在函数外部定义它们。这通常涉及Global Variables
,但这不是污染全球范围的良好做法。
Alternate是封装函数registerEvents()
内的所有事件绑定,如果你在事件处理程序之外定义一个变量,你可以在处理程序之间访问它们,但它仍然是函数的一部分,因此可以防止污染。这也有助于将所有绑定保持在一起,使调试更容易。
function registerEvents() {
var sharableVar = null;
$("#btnInit").on("click", function() {
sharableVar = Math.floor(Math.random() * 10);
});
$("#btnNotify").on("click", function() {
console.log(sharableVar);
})
}
registerEvents()
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<button id="btnInit">Initialize</button>
<button id="btnNotify">Notify</button>
&#13;
答案 2 :(得分:1)
尝试使用在全局范围内声明的数组。
/* iterate over all documents in collection */
FOR doc IN collection
LET s = (
/* iterate over embedded attribute "data */
FOR sub IN doc.data
/* look at QID at array position 0 */
FILTER sub[0].QID == 2
/* first match is sufficient */
LIMIT 1
/* return SubAID value from array position 1 */
RETURN sub[1].SubAID
)
/* only return documents with a match */
FILTER LENGTH(s) > 0
/* return first result from subquery (subquery result is always an array) */
RETURN s[0]
OR
使用会话存储来设置/获取值。
var TsearchResultIDArray = []; //declaration of array
$(document).on('click', '.lister ul span', function(event) {
QueryGetta1()
.done(function(data) {
TsearchResultIDArray.push = data.items[0].id.videoId;//push the value to the array
var target = '<div class="slideYThumbnail"><img class="slideYThumbnailInside" src="http://img.youtube.com/vi/' + TsearchResultID + '/0.jpg"></img></div>';
});
});
$(document).on('click', '.slideYThumbnail', function(event) {
alert(TsearchResultIDArray[0]); // access the first element of the array.
});