我正在尝试开发一个循环图像滑块,并有一个关于我正在引用开发的文档的问题。
JQuery 函数实际上并不调用选择器,我不确定如何读取它。
$.fn.cycle = function(options, arg2) {
var o = { s: this.selector, c: this.context };
上面的脚本在我的 javascript 文档中,下面的方法在我的 HTML 文档中调用上面的脚本。
$(document).ready(function() {
$('.headline').cycle({
fx: 'fade', // choose your transition type, ex: fade, scrollUp, shuffle,
cleartypeNoBg:true
});
.headline 是在 HTML 文档中定义的类。我很困惑,因为它有一个选择器而 $.fn.cycle 没有。
.headline 是否将值传递给 .fn?如果是这样,它是如何只传入变量的那部分的?
如果您想查看完整的 JQuery 功能,请点击此处:
$.fn.cycle = function(options, arg2) {
var o = { s: this.selector, c: this.context };
// in 1.3+ we can fix mistakes with the ready state
if (this.length === 0 && options != 'stop') {
if (!$.isReady && o.s) {
log('DOM not ready, queuing slideshow');
$(function() {
$(o.s,o.c).cycle(options,arg2);
});
return this;
}
// is your DOM ready? http://docs.jquery.com/Tutorials:Introducing_$(document).ready()
log('terminating; zero elements found by selector' + ($.isReady ? '' : ' (DOM not ready)'));
return this;
}
// iterate the matched nodeset
return this.each(function() {
var opts = handleArguments(this, options, arg2);
if (opts === false)
return;
opts.updateActivePagerLink = opts.updateActivePagerLink || $.fn.cycle.updateActivePagerLink;
// stop existing slideshow for this container (if there is one)
if (this.cycleTimeout)
clearTimeout(this.cycleTimeout);
this.cycleTimeout = this.cyclePause = 0;
var $cont = $(this);
var $slides = opts.slideExpr ? $(opts.slideExpr, this) : $cont.children();
var els = $slides.get();
if (els.length < 2) {
log('terminating; too few slides: ' + els.length);
return;
}
var opts2 = buildOptions($cont, $slides, els, opts, o);
if (opts2 === false)
return;
var startTime = opts2.continuous ? 10 : getTimeout(els[opts2.currSlide], els[opts2.nextSlide], opts2, !opts2.rev);
// if it's an auto slideshow, kick it off
if (startTime) {
startTime += (opts2.delay || 0);
if (startTime < 10)
startTime = 10;
debug('first timeout: ' + startTime);
this.cycleTimeout = setTimeout(function(){go(els,opts2,0,(!opts2.rev && !opts.backwards))}, startTime);
}
});
最佳答案
选择器是插件中的 this
。
例如:
$.fn.cycle = function() {
console.log(this);
};
$('.headline').cycle(); //logs the `.headline` jQuery element
见 fiddle :http://jsfiddle.net/maniator/eE6q2/
关于javascript - 没有选择器的 JQuery?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9483886/