[英]How to pass user arguments to a JS script to be run?
So I have this slider script that runs on page load and this slider can take many options and the code looks something like this: 因此,我有一个在页面加载时运行的滑块脚本,并且此滑块可以采用许多选项,并且代码如下所示:
$( '.slider' ).each( function() {
var $height = 100; // default
var $width = 100; // default
$( this ).slider( {
height: $height,
width: $width
} );
} );
So if I wanted the option to be able to allow users to pass in their own arguments for width and height WITHOUT having to rerun the script, how can that be done? 因此,如果我希望该选项能够允许用户传递自己的宽度和高度参数而不必重新运行脚本,那该怎么办呢? So basically I want to create a HOOK to use the user passed arguments if any otherwise use default. 因此,基本上我想创建一个HOOK来使用用户传递的参数(如果有的话)使用默认值。
Thanks for looking. 感谢您的光临。
$( '.slider' ).each( function() {
var $height = prompt("Please enter the height:","Your Height");
var $width = prompt("Please enter the width:","Your Width");
$( this ).slider( {
height: $height,
width: $width
} );
} );
Or if all .slide elements have unique height/width (this avoid multiple pop up on the user browser) 或者,如果所有.slide元素都具有唯一的高度/宽度(这样可以避免在用户浏览器中多次弹出)
var slideHeight=prompt("Please enter the height:","Your Height");
var slideWidth=prompt("Please enter the Width:","Your Width");
$( '.slider' ).each( function() {
var $height = parseInt(slideHeight)>0?parseInt(slideHeight):100;
var $width =parseInt(slideWidth)>0?parseInt(slideWidth):100;
$( this ).slider( {
height: $height,
width: $width
} );
} );
I would recommend building this as a jQuery plugin since it's a UI widget and you're already using jQuery. 我建议将其构建为jQuery插件,因为它是一个UI小部件,并且您已经在使用jQuery。 The jQuery website has a section on how to make plugins. jQuery网站上有一节介绍如何制作插件。 The tutorial will show you how to set up your plugin to have default values which can be overridden by passing in optional params. 本教程将向您展示如何将插件设置为具有默认值,这些默认值可以通过传入可选参数来覆盖。
Maybe that can be done by using a global variable (the slideropts
object) like 也许可以通过使用全局变量( slideropts
对象)来完成
$( '.slider' ).each( function() {
slideropts={height:100,width:100};
$( this ).slider( slideropts );
} );
Note: there is no var
before the slideropts
so this variable will have global scope and can be changed at any time by any other JS routine by doing something like 注意: slideropts
之前没有var
,因此此变量将具有全局作用域,并且可以通过执行任何其他操作来随时更改任何其他JS例程
slideropts.height=120;
All unchanged options will simply keep their old values. 所有未更改的选项将仅保留其旧值。
I am assuming by "user" you mean the reuser of your script, not the visitor of the page. 我假设“用户”是指脚本的重用者,而不是页面的访问者。 One way is to let him set a global option which is used as default. 一种方法是让他设置用作默认值的全局选项。
var options = $.extend({}, {height: 100, width: 100}, $.slideroptions);
$('.slider').each(function() {
$(this).slider(options);
});
A somewhat more complicated but more maintainable option is to use a trigger, as you mentioned. 如您所述,一个更复杂但更可维护的选项是使用触发器。
var event = new $.Event('slider.init');
event.options = {height: 100, width: 100};
$(window).trigger(event);
$('.slider').each(function() {
$(this).slider(event.options);
});
The user can then override in an event handler: 然后,用户可以在事件处理程序中重写:
$(window).on('slider.init', function(event) {
event.options.height = 90;
});
Alternatively, you can make the decision per-slider: 或者,您可以按滑块决定:
$('.slider').each(function() {
var event = new $.Event('slider.init');
event.options = {height: 100, width: 100};
$(this).trigger(event);
$(this).slider(event.options);
});
$(window).on('slider.init', function(event) {
if ($(this).is('.long-slider') {
event.options.height = 90;
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.