码迷,mamicode.com
首页 > Web开发 > 详细

js的一些东西

时间:2017-10-16 13:49:22      阅读:232      评论:0      收藏:0      [点我收藏+]

标签:include   i++   index   name   prototype   typeof   nbsp   query   prot   

扩展jquery方法

1.$.extend() 为jQuery类添加添加类方法,可以理解为添加静态方法

例如:

$.extend({  
includePath: ‘‘,
include: function(file) {
var files = typeof file == "string" ? [file]:file;
for (var i = 0; i < files.length; i++) {
var name = files[i].replace(/^\s|\s$/g, "");
var att = name.split(‘.‘);
var ext = att[att.length - 1].toLowerCase();
var isCSS = ext == "css";
var tag = isCSS ? "link" : "script";
var attr = isCSS ? " type=‘text/css‘ rel=‘stylesheet‘ " : " language=‘javascript‘ type=‘text/javascript‘ ";
var link = (isCSS ? "href" : "src") + "=‘" + $.includePath + name + "‘";
if ($(tag + "[" + link + "]").length == 0) document.write("<" + tag + attr + link + "></" + tag + ">");
}
}
});

使用方法:$.include([‘static/js/test.js‘])

2.$.fn   

$.fn是指jquery的命名空间,加上fn上的方法及属性,会对jquery实例每一个有效

 

为数组添加自定义的方法:

如添加remove方法:

Array.prototype.remove = function() {
var what, a = arguments, L = a.length, ax;
while (L && this.length) {
what = a[--L];
while ((ax = this.indexOf(what)) !== -1) {
this.splice(ax, 1);
}
}
return this;
};

 

js的一些东西

标签:include   i++   index   name   prototype   typeof   nbsp   query   prot   

原文地址:http://www.cnblogs.com/luoyunshu/p/7676301.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!