请问js怎么对function 扩展 本帖最后由 nobnobaj 于 2010-05-21 15:23:23 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 var $=function(id){return document.getElementById(id)};$.msg=function(str){ alert(str);};$.msg("dddd"); $.fn.msg=function(str){ alert(str)}或者$.extend.msg=function(str){ alert(str)} 我可能没写明白我的意思是 对不同对象 $(id) 进行对应的操作 就是类似jquery里$('a').bind(....)$('b').bind(....) <input id=aaa value="test"><script>var $=function(id){ this.o=document.getElementById(id);}$.prototype.msg=function(str){ alert(str);};var obj= new $('aaa');obj.msg("sdf");alert(obj.o.value);</script> var $=function(id){ return document.getElementById(id) //这里改成this.dom = document.getElementById(id)}$.prototype.msg=function(str){ alert(str)}var tmp = new $(id) //调用的时候使用new构造器函数调用tmp.dom//你的这个元素tmp.msg() //你拓展的方法 to cj205请问有没有办法 new $(id)封装起来?每次都new一下 比较麻烦 http://www.v-ec.com/dh20156/article.asp?id=157看看这个是否对你有帮助 你要封装就跟$一样啊var $=function(id){ this.o=document.getElementById(id);}$.prototype.msg=function(str){ alert(str);};var $$ = function(id){return new $(id);}$$('aaa').msg("sdf");alert($$('aaa').o.value); 明白你的意思了,和jq理念一样, 就是对结构进行样式和行为的操作。dom.doSomeWorks()。可以Google一下 “Query工作原理”<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><style type="text/css"></style><title></title></head><body><a onclick="foo1()" id="a1">点击绑定事件</a><div id="test"> <ul> <li>65</li> <li id="dd">65</li> </ul></div></body><script type="text/javascript">var MyQuery = function(selector){ if ( window == this ) return new MyQuery(selector); var dom = document.getElementById(selector);//这里只写byid,更复杂的选择器,建议研究jq源码 var arr = [dom]; return this.setArray(arr);}MyQuery.prototype.setArray = function( arr ) { this.length = 0; [].push.apply( this, arr ); return this;}MyQuery.fn = MyQuery.prototype;var $ = MyQuery;//插件扩展MyQuery.fn.each = function(method){ for(var i=0,l=this.length; i<l; i++){ method.call(this[i],i); }}MyQuery.fn.bind=function(type,eventHandle){//绑定事件 this.each(function(i){ if(document.addEventListener){ this.addEventListener(type,eventHandle,false); } else { this.attachEvent('on'+type,eventHandle); } });}MyQuery.fn.other=function(){ this.each(function(i){ //do other });}//绑定句柄function foo1(){ $('dd').bind("click",function(){ alert("绑定") })}</script></html> 执行 $('aaa').msg('dfdffdf') 提示错误这个$ 怎么绑定msg啊$ != $('aaa') var element = function(id) { this._element = document.getElementById(id); return this; } element.prototype.msg = function(str) { alert(str); } var $ = function(id) { return new element(id); } $('t').msg('ddd'); jquery的ajax $.get方法的返回值问题 Microsoft JScript 运行时错误: 对象不支持此属性或方法 EXTJS GridPanel如何设置多选 IE8下无法获取windows对象 如何实现在用户等待数据处理过程中,显示出正在处理的数据和页面?? 请问如何在firefox中获知键盘上所按下的哪个键 关于zTree 展开一个节点,然后关闭上次打开的节点的问题。 如何将动态添加表格的javascript改为Vbscript? 能不能检测客户端的cookie有无设置?? 选定内容判断问题 网站开发WEB前端高性能优化之JavaScript优化细节 这2个区别在哪里啊
$.msg=function(str){
alert(str);
};
$.msg("dddd");
$.fn.msg=function(str){
alert(str)
}或者
$.extend.msg=function(str){
alert(str)
}
$('a').bind(....)
$('b').bind(....)
<script>
var $=function(id){
this.o=document.getElementById(id);
}
$.prototype.msg=function(str){
alert(str);
};var obj= new $('aaa');
obj.msg("sdf");
alert(obj.o.value);
</script>
return document.getElementById(id) //这里改成this.dom = document.getElementById(id)
}
$.prototype.msg=function(str){
alert(str)
}var tmp = new $(id) //调用的时候使用new构造器函数调用
tmp.dom//你的这个元素
tmp.msg() //你拓展的方法
this.o=document.getElementById(id);
}
$.prototype.msg=function(str){
alert(str);
};var $$ = function(id){return new $(id);}$$('aaa').msg("sdf");
alert($$('aaa').o.value);
dom.doSomeWorks()。
可以Google一下 “Query工作原理”<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
</style>
<title></title></head><body>
<a onclick="foo1()" id="a1">点击绑定事件</a>
<div id="test">
<ul>
<li>65</li>
<li id="dd">65</li>
</ul>
</div>
</body>
<script type="text/javascript">
var MyQuery = function(selector){
if ( window == this ) return new MyQuery(selector);
var dom = document.getElementById(selector);//这里只写byid,更复杂的选择器,建议研究jq源码
var arr = [dom];
return this.setArray(arr);
}
MyQuery.prototype.setArray = function( arr ) {
this.length = 0;
[].push.apply( this, arr );
return this;
}
MyQuery.fn = MyQuery.prototype;
var $ = MyQuery;//插件扩展
MyQuery.fn.each = function(method){
for(var i=0,l=this.length; i<l; i++){
method.call(this[i],i);
}
}
MyQuery.fn.bind=function(type,eventHandle){//绑定事件
this.each(function(i){
if(document.addEventListener){
this.addEventListener(type,eventHandle,false);
} else {
this.attachEvent('on'+type,eventHandle);
}
});
}
MyQuery.fn.other=function(){
this.each(function(i){
//do other
});
}//绑定句柄
function foo1(){
$('dd').bind("click",function(){
alert("绑定")
})}</script>
</html>
这个$ 怎么绑定msg啊$ != $('aaa')
var element = function(id) {
this._element = document.getElementById(id);
return this;
}
element.prototype.msg = function(str) {
alert(str);
}
var $ = function(id) {
return new element(id);
}
$('t').msg('ddd');