本人初次接触封装jquery插件,但一直报**$(…).plugIn is not a function**。百度一直没解决,后来自行找到原因。
iptMenu.html
<html>
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="static/jquery-1.4.1.min.js"></script>
<script src="static/iptMenu.js" type="javascript"></script>
</head>
<body>
<input type="number" id="ipt" value="10" style="margin-top: 200px"/>
<script>
$(function(){
$('#ipt').plugIn({
event : 'click' //可进行篡改
});
});
</script>
</body>
</html>
iptMenu.js
;(function($){
$.fn.plugIn = function(options){
var defaults = {
event : 'click'
//各种默认参数
}
var options = $.extend(defaults,options); //传入的参数覆盖默认参数
this.each(function(){
var _this = $(this); //缓存一下插件传进来的节点对象。
//执行内容
console.log(123456);
alert(_this.attr("id"));
});
return $(this); //把节点对象返回去,为了支持链式调用。
}
})(jQuery);
问题就出在了 :
<script src="static/iptMenu.js" type="javascript"></script>
修正后:
<script src="static/iptMenu.js"></script>
问题解决,这个问题缠绕了我一天多,特此记录。