首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >未定义Jquery myfunction。

未定义Jquery myfunction。
EN

Stack Overflow用户
提问于 2016-01-08 17:59:08
回答 4查看 67关注 0票数 0
代码语言:javascript
复制
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作为函数,它包含在不同事件上,但是控制台返回未定义的ReferenceError: MonPlugin。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2016-01-08 18:04:29

代码语言:javascript
复制
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 );
代码语言:javascript
复制
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

票数 0
EN

Stack Overflow用户

发布于 2016-01-08 18:03:32

打电话

代码语言:javascript
复制
jQuery(".titre1").bind("click",$.fn.MonPlugin);

而不是

代码语言:javascript
复制
jQuery(".titre1").bind("click",MonPlugin);
票数 3
EN

Stack Overflow用户

发布于 2016-01-08 18:02:32

使用这种方式创建函数:

代码语言:javascript
复制
var MonPlugin = function() {
  jQuery(".size-60").css( widescreen )
};

代码语言:javascript
复制
function MonPlugin() {
  jQuery(".size-60").css( widescreen )
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34683231

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档