jquery拓展方法,在jq load方法里找不到拓展的方法

$.fn.test添加一个方法,在$(function(){})之外可以正常使用,在 $(function(){})里 $().test为undefined

相关代码

//引入js
$.fn.toastmessage = function (method) {

    // Method calling logic
    if (methods[method]) {
        return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
    } else if (typeof method === 'object' || !method) {
        return methods.init.apply(this, arguments);
    } else {
        $.error('Method ' + method + ' does not exist on jQuery.toastmessage');
    }
};
//页面行内script
var $message = $()
$(function () {
    console.log($().toastmessage);
    console.log($message.toastmessage);

你期待的结果是什么?实际看到的错误信息又是什么?

console.log($().toastmessage); //undefind
console.log($message.toastmessage);//正常 function

为什么在  $(function () {下 jq实例找不到继承的方法
阅读 1.4k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题