简体   繁体   English

如何将用户参数传递给要运行的JS脚本?

[英]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.

相关问题 如果客户端通过套接字连接,如何运行脚本并传递参数? - How to run script and pass arguments if client is connected over socket? Adobe After Effects扩展脚本 - 如何从命令行运行脚本并传入参数? - Adobe After Effects extend script - how to run script from command line and pass in arguments? JS脚本如何从Flutter Web传递到html文件(HTMLElement,iframe)arguments? - How to pass from Flutter Web to html file (HTMLElement, iframe) arguments for the JS script? 如何在PhantomJS脚本中将customHeaders作为系统参数传递? - How to pass customHeaders as system arguments in PhantomJS script? 如何使用 Google Apps Script 中的 google.script.run 将 JS 对象传递给服务器函数? - How to pass a JS object to a server function using google.script.run in Google Apps Script? 继承原型JS时如何传递参数? - How to pass arguments when inheriting prototypes JS? 如何将参数传递给 webpack.conf.js? - How to pass arguments to webpack.conf.js? 如何将参数/参数传递给Node JS中的函数 - How to pass parameter/arguments to functions in node js 将参数传递给目标脚本 - pass arguments to target script 如何在v8中将命令行参数传递给脚本? - How to pass command line arguments to script in v8?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM