简体   繁体   English

如何从 jquery 插件元素上的单击事件获取返回值?

[英]How to get a return value from click event on a jquery plugin element?

I am creating a plugin which will be attached to a div and show up with few buttons.我正在创建一个插件,该插件将附加到一个 div 并显示几个按钮。 Now when these buttons are clicked, it will perform some calculation and return a value which will be stored in variable.现在,当单击这些按钮时,它将执行一些计算并返回一个将存储在变量中的值。 Below is the probable scenario.下面是可能的场景。

  $(document).ready(function()
  {
     /*---- Custom plugin is initialized ---------*/ 
     $('.mydiv').myPlugin();

      /*----- Click event on the buttons created by the plugin and attached to the div -----*/
      $('body').on('clicked','.myPluginButton',function()
      {
        var returnedValue = /* Here plugin calculated some data and returns a value which will be stored in this returnedValue variable for later use */
      });
   });

Please suggest the way forward for this problem.请提出解决此问题的方法。 Thanks in advance.提前致谢。

In short, you'll need something like this:简而言之,你需要这样的东西:

$('.mydiv').myPlugin({ // Pass options Object to plugin
  clicked: function(ev, data) {
    console.log(data); // Example accessing data Object
  }
});

Since you say your buttons are created by your plugin, let it be so!既然你说你的按钮是由你的插件创建的,那就这样吧!
Instead of creating listeners to 'body' :而不是为'body'创建侦听器:

  • Create a plugin options Method (callback) called clicked创建一个名为clicked插件选项方法(回调)
  • Use .call() to pass this , ...arguments , and a data Object使用.call()传递this...arguments和一个data对象
  • Use your plugin methods when instantiating your plugin实例化插件时使用插件方法

 $.fn.myPlugin = function(opt) { const settings = $.extend({ namespace: 'myPlugin', clicked: function() {}, }, opt); this.each(function(index, el) { const $el = $(el); // Your target element const data = { // Some data you want to pass on click index: index, el: el, $el: $el, test: `This is ${settings.namespace} ${index}` }; // The button created by the plugin and attached to the Element $('<button/>', { appendTo: $el, class: settings.namespace+'-button', text: 'CLICK ME', }).on(`click.${settings.namespace}`, function() { if (settings.clicked && typeof settings.clicked === 'function') { return settings.clicked.call(this, ...arguments, data); } }); }); return this; }; jQuery(function($) { // DOM ready and $ alias in scope // Custom plugin is initialized $('.mydiv').myPlugin({ // Click event on the buttons created by the plugin and attached to the div clicked: function(ev, data) { console.log(ev.type); // Example accessing $Event Object console.log(this.textContent); // Accessible this reference to Button console.log(ev.target.tagName); // Example accessing target Element (Button) console.log(data.test); // Example accessing data console.log(`Parent ID is: ${data.el.id}`); // Example accessing parent ID from data } }); });
 <div class="mydiv" id="mydiv_1">THIS IS MYDIV_1</div> <div class="mydiv" id="mydiv_2">THIS IS MYDIV_2</div> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

Or with less code for sake of readability, and by using the jQuery.Event.data Object:或者为了可读性而使用更少的代码,并使用 jQuery.Event.data 对象:

 $.fn.myPlugin = function(opt) { const settings = $.extend({ clicked: function() {}, }, opt); return this.each(function(index, el) { const data = { index: index, text: `Clicked ${index}` }; $('<button/>', { appendTo: el, class: 'myPlugin-button', text: 'CLICK', }).on('click', data, settings.clicked); }); }; $('.mydiv').myPlugin({ clicked: function(ev) { console.log(ev.data.index); console.log(ev.data.text); } });
 <div class="mydiv" id="mydiv_1">THIS IS MYDIV_1</div> <div class="mydiv" id="mydiv_2">THIS IS MYDIV_2</div> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM