分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 技术分享

js的一些东西

发布时间:2023-09-06 01:18责任编辑:苏小强关键词:js

扩展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的一些东西

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

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved