使用.on()事件的变量,自定义jQuery插件中的侦听器不会触发

马修·约翰逊(Matthew Johnson)

我正在将这个问题的答案纺到jQuery插件中,但是我遇到了问题。我在.on()中使用变量作为事件,但是监听器从不触发。

jQuery的

(function ($) {
    $.fn.randomLetterStyles = function( options ) {

      var settings = $.extend({
          colors: ["#ddd", "#333", "#999", "#bbb"],
          sizes:["12"],
          type:"hover",
          defaultColor: "#999",
          defaultSize: "12"
        }, options );

      //both of these appear in console
      console.log("check 1");
      console.log(settings.type);

        $(this).on(settings.type, function() {
          //this does not appear in console
          console.log("check 2");

          wrapLetters(this);
          $('.random-styles', this).css('color', function(){
              var idx = Math.floor(Math.random() * settings.colors.length);
              return settings.colors[idx];
          });
          $('.random-styles', this).css('color', function(){
              var idx = Math.floor(Math.random() * settings.sizes.length);
              return settings.sizes[idx];
          });
          $('.random-styles', this).css('font-size', function(){
              var idx = Math.floor(Math.random() * settings.sizes.length);
              return settings.sizes[idx];
          });
      }, function(){
          $('.random-styles', this).css({'color':settings.defaultColor, 'font-size':settings.defaultSize});     
  }); 
};


//Recursive function by Logan Smyth
// Wrap every letter in a <span> with .random-color class.
function wrapLetters(el){
  if ($(el).hasClass('random-styles')) return;

  // Go through children, need to make it an array because we modify
  // childNodes inside the loop and things get confused by that.
  $.each($.makeArray(el.childNodes), function(i, node){
    // Recursively wrap things that aren't text.
    if (node.nodeType !== Node.TEXT_NODE) return wrapLetters(node);

    // Create new spans for every letter.
    $.each(node.data, function(j, letter){
        var span = $('<span class="random-styles">').text(letter);
        node.parentElement.insertBefore(span[0], node);
    });

        // Remove old non-wrapped text.
        node.parentElement.removeChild(node);
      });
    }
} ( jQuery ));

$(document).ready(function() {
    $("#firstBox").randomLetterStyles();
    $("#secondBox").randomLetterStyles();
    $("#thirdBox").randomLetterStyles();
});

的HTML

<div id="firstBox">This box works on hover!</div>
<div id="secondBox">This box works on click!</div>
<div id="thirdBox">This box has custom settings!</div>

小提琴

爆米花

.on('hover', function)不像.hover(function, function)-它不接受两个功能。删除第二个事件,然后触发事件。

http://jsfiddle.net/U78RV/4/

您仍然可以创建这两个事件

$("#firstBox").randomLetterStyles({type: 'mouseover', ...}).randomLetterStyles({type: 'mouseout', ...});

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用事件侦听器响应自定义下拉列表

使用es6模块创建自定义“事件侦听器”

NodeJS中使用require('events')的自定义事件侦听器。

如何使用 Mqtt 编写自定义变量更改侦听器

使用自定义适配器在AlertDialog中单击侦听器

使用事件侦听器进行反应的自定义钩子在更新状态时不起作用

在自定义视图上使用自定义侦听器进行数据绑定

使用事件侦听器在不使用jquery的情况下触发keypress事件

使用Firebase进行自定义登录将完整侦听器添加到活动中

是否使用“ .click()”异步触发事件侦听器?

使用带有参数的自定义绑定适配器侦听器方法的Android自定义视图

使用自定义对话框类时如何修改文本并单击侦听器?

Kotlin如何通过lambda使用自定义单击侦听器,而不是声明对象:CustomClass

网格组件和使用自定义排序侦听器进行排序

无法使用点击侦听器创建自定义按钮

jQuery自定义事件侦听器

ContextStartedEvent未在自定义侦听器中触发

清理自定义元素中的事件侦听器

使用Jquery禁用文本框内的事件侦听器

如何使用jquery .each()添加单个事件侦听器?

使用jQuery将事件侦听器添加到动态添加的元素中

使用自定义事件触发点击事件监听器

单击事件侦听器和函数上的 JQuery,使用不同的参数以不同的方式触发?

类似于DSL的OnClick侦听器,可在Android中使用Kotlin进行自定义视图

使用自定义事件对象触发事件处理程序

如何设置事件侦听器并使用react钩子在首次触发事件后将其删除?

即使使用 useEffect 钩子的返回回调,也不会在 React 中删除事件侦听器

使用Composition API插件在Vue 2中功能组件(在正常工作时)发生未定义的侦听器错误

使用自定义渲染器时未触发Xamarin Forms Switch的Toggled事件