将 jQuery 函数集成到选项卡 UI 中的另一个函数中
我正在尝试将一个功能集成到另一个功能中,但我很困惑。
我想做的是添加一个 jQuery 函数来调用随机引用,并且我希望该函数在 jQuery Tabs UI 中的选项卡更改时触发。
这是随机引用函数:
$.get('quotes.txt', function(data) {
var quotes = data.split("\@");
var idx = Math.floor(quotes.length * Math.random());
$('.quotes').html(quotes[idx]);
});
下面是选项卡初始化(以及另一个在选项卡更改时折叠 div 的函数):
$(document).ready(function() {
var $tabs= $("#tabs").tabs({
fx : {
opacity: 'toggle'
},
select : function(event, ui) {
$(".entry-content").hide();
} //I assume it goes near here, but no luck
});
});
引号函数是否需要首先调用变量?
并且,如何使引用 div 在更改引用时也使用 fx 不透明度效果?
编辑 4/27/11
这可以工作并在函数中使用 fx 效果:
$(document).ready(function () {
var $tabs = $("#tabs").tabs({
fx: {
opacity: 'toggle'
},
select: function (event, ui) {
$(".entry-content").hide();
$('div#quotescontainer').load('quotes.html', function () {
var $quotes = $(this).find('div.quote');
var n = $quotes.length;
var random = Math.floor(Math.random() * n);
$quotes.hide().eq(random).fadeIn();
});
}
});
});
I'm trying to integrate one function into another function and am puzzled.
What I'm trying to do is add a jQuery function that will call a random quote, and I want that function to fire on a tab change in jQuery Tabs UI.
This is the random quote function:
$.get('quotes.txt', function(data) {
var quotes = data.split("\@");
var idx = Math.floor(quotes.length * Math.random());
$('.quotes').html(quotes[idx]);
});
And below this is the tabs init (along with another function that collapses divs when a tab is changed):
$(document).ready(function() {
var $tabs= $("#tabs").tabs({
fx : {
opacity: 'toggle'
},
select : function(event, ui) {
$(".entry-content").hide();
} //I assume it goes near here, but no luck
});
});
Does the quotes function need to have the variables called first?
And, how would it work to make the quote div also use the fx opacity effect when it changes quotes?
Edit 4/27/11
This works and uses the fx effect in the function:
$(document).ready(function () {
var $tabs = $("#tabs").tabs({
fx: {
opacity: 'toggle'
},
select: function (event, ui) {
$(".entry-content").hide();
$('div#quotescontainer').load('quotes.html', function () {
var $quotes = $(this).find('div.quote');
var n = $quotes.length;
var random = Math.floor(Math.random() * n);
$quotes.hide().eq(random).fadeIn();
});
}
});
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
代码应该放在大括号内,
就我个人而言,我会将其放在选项卡的“show”事件中,但这应该不是问题。
我已经用一些测试代码设置了一个小提琴,供您查看它应该如何: http://jsfiddle.net /PyN5Y/1/
The code should go inside the curly braces
Personally, I'd put it in the 'show' event of the tab, but that shouldn't be a problem.
I've setup a fiddle with some test code for you to see how it should be : http://jsfiddle.net/PyN5Y/1/
以下是我如何监视选项卡上的更改,然后启动您的报价功能。
update_quote 函数可以清理并变得更简单,我将其保留得非常详细,以便您可以了解如何更新它。你也可以链接你的 jquery 效果。
Here is how I would monitor for a change on the tabs and then kick off your quote function.
The update_quote function could be cleaned up and made simpler, I kept it very verbose so you could see how to update it. You can chain your jquery effects also.