Jquery myfunction没有定义

时间:2016-01-08 17:59:08

标签: javascript jquery

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。

4 个答案:

答案 0 :(得分:3)

呼叫

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

而不是

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

答案 1 :(得分:1)

使用这种方式创建函数:

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

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

答案 2 :(得分:0)

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>

答案 3 :(得分:0)

custom jquery functionjquery function extenders只能与jquery object一起使用。因此,为了调用自定义函数,您需要将其附加到元素。

$.fn.MonPlugin = function() 
{
    alert();
}

$(".titre1").bind("click",function()
{
  $(this).MonPlugin();
})

示例:http://jsfiddle.net/DinoMyte/bvtngh57/149/