插件初体验,用jQuery建立一个弹出框对象
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script type="text/javascript" src="js/jquery-3.2.1.js" ></script>
<script type="text/javascript">
(function(jQuery){
$.alert=function(msg){
window.alert(msg);
}
$.fn.alert=function(msg){
window.alert(msg);
}
})(jQuery);
$().ready(function(){
$.alert("aaa");
$("body").alert("bbb");
});
</script>
</head>
<body>
</body>
</html>
自己制作一个循环回掉
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script type="text/javascript" src="js/jquery-3.2.1.js" ></script>
<script type="text/javascript">
(function(jQuery){
jQuery.fn.myeach=function(callback){
var array=$(this);
for(var i=0;i<array.length;i++)
{
callback.call($(array[i]),$(array[i]));
}
}
jQuery.myeach=function(obj,callback)
{
var array=obj;
for(var i=0;i<array.length;i++)
{
callback.call($(array[i]),$(array[i]));
}
}
})(jQuery);
$().ready(function(){
$("p").myeach(function(){
alert($(this).text());
});
$.myeach($("p"),function(e){
alert($(e).text());
});
});
</script>
</head>
<body>
<p>sdfdsf</p>
<p>sdfdsf</p>
<p>sdfdsf</p>
</body>
</html>
自己写一个标签的方法
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script type="text/javascript" src="js/jquery-3.2.1.js" ></script>
<script type="text/javascript">
(function(jQuery){
jQuery.fn.myextend=function(json)
{
for(var i in json)
{
jQuery.fn[i]=json[i];
}
}
jQuery.myextend=function(json){
for(var i in json)
{
jQuery[i]=json[i];
}
}
})(jQuery);
$().ready(function(){
$("body").myextend({aa:function(){
alert("aa");
}})
$("body").aa();
});
</script>
</head>
<body>
</body>
</html>