开发者问题收集

Jquery myfunction 未定义

2016-01-08
73
var widescreen = {
  "flex": "0 100%",
  "width": "100%",
  "-webkit-flex": "0 100%",
  "-moz-flex": "0 100%",
  "-ms-flex": "0 100%",  
};

jQuery.fn.MonPlugin=function() {
jQuery(".size-60").css(widescreen)
};

jQuery(".titre1").bind("click",MonPlugin);

我想使用 MonPlugin 作为包含在不同事件中的函数,但控制台返回 Uncaught ReferenceError: MonPlugin 未定义。

3个回答

拨打

jQuery(".titre1").bind("click",$.fn.MonPlugin);

而不是

jQuery(".titre1").bind("click",MonPlugin);
Nigrimmist
2016-01-08

使用此方式创建函数:

var MonPlugin = function() {
  jQuery(".size-60").css( widescreen )
};

function MonPlugin() {
  jQuery(".size-60").css( widescreen )
}
Norlihazmey Ghazali
2016-01-08
var widescreen = {
  "flex": "0 100%",
  "width": "100%",
  "-webkit-flex": "0 100%",
  "-moz-flex": "0 100%",
  "-ms-flex": "0 100%",
};

// a plugin declared like this is added to jQueryies prototype allowing
// you to add the method to the chain of functions. but you would need to modify
// your code just a tad.

jQuery.fn.MonPlugin = function() {
  // plugins have the collection of the selected elements so loop through them all
  this.each(function(){
    // create a new jQuery object with the current element and bind your function
    jQuery(this).on('click', MonPluginCallBack );
};

// to call your plugin you would use
jQuery('.titre1').MonPlugin();


// it seems that you just want a callback function so you should use

function MonPluginCallBack( event ){
  jQuery(".size-60").css(widescreen);
}

// here you are passing the function by reference
jQuery(".titre1").bind("click", MonPluginCallBack );
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
synthet1c
2016-01-08