解决方案 »
- ASP获取JavaScript的值的方法
- jsp 多选 后 可进行不同的操作
- 一个居右底的div
- 刚学 javascript 我想实现 fromlist的 多项选择 一下能 加好几个 一下能删除 好几个 的 效果 我的代码在哪改一下呢? 请高手给该一下被?
- 为何ext的grid显示的表格里没数据呢?
- 求助:JavaScript中无法使用FileSystemObject
- 关于iframe 主窗口刷新!
- 急!javascrip如何调用word控件,使它具有将word文档转成html的功能???
- 如何实现信息反馈?
- ie6中js文件导入失败问题
- 跨页面运行脚本的问题
- javascript如何获取text控件的值
你没明白我说的是什么! 我说的不是用jquery 是自己写一个$().test()去实现上面的功能
1、全局插件,例如你的$.test();
2、对象方法插件,这个比较常见;
3、选择器插件,用的比较少,建议去研究研究
$.test()这个真心不方便,因为打算封装好以后就不碰DOM命令了,但是$.test()这么一写,因为在test方法里还要写DOM命令所以觉得特别不方便。
所以想知道jquery那种 $().X() 是怎么实现的.
function ClassObj(id)
{
this.dom = document.getElementById(id);
}
ClassObj.prototype.html = function (e)
{
if(e)
{
this.dom.innerHTML = e;
return this;
}
else
return this.dom.innerHTML;
}var $ = function (id)
{
return new ClassObj(id);
};
</script><p id='asd'>求助</p>
<input type="button" value="xxxxxxxxxxx" onclick="$('asd').html('123456')" />
这个直接修改 $的propotype,以后所有的$().就都有这个方法了。
不过不建议这样添加,主要是多人开发时常会有命名冲突,或者有一个统一的添加fn的决策机制以防止此类现象。
js新手,求详细注释,谢谢,我对对象的概念不是很理解. propotype这个东西,上网查过,不过各位大神说的昏天暗地的,实在不理解,求个简单易懂的解释,谢谢
(function(window){
_$ = window.$;
$ = function(o){
if(Boolean(o) == false)
return event.srcElement;
else
return typeof(o) == "string"?document.getElementById(o):o;
};
$.test=function(str){
document.write('郁闷蛋疼'+str);
}
}(window))
上面的$.test()可以执行;但是换成$().test()就不行 ,求解
(function(window){
_$ = window.$;
$ = function(o){
if(Boolean(o) == false)
return event.srcElement;
else
return typeof(o) == "string"?document.getElementById(o):o;
};
$.test=function(str){
document.write('郁闷蛋疼'+str);
}
}(window))
上面的$.test()可以执行;但是换成$().test()就不行 ,求解
不是说了么。$()函数要返回对象的实例
this.name= "A";
this.show = function(){
alert(this.name);
}
}function B (){
this.name= "B";
}
B.prototype.show = function(){
alert(this.name);
}在这里,你new A(); new B();他们的show()方法都是一样的,不过一般推荐的写法是第二个。好处一个是节约内存,另一个扩展方便,你可以写个基本类,然后在别的文件里对这个类进行扩展,而且可以直接修改javascript内置对象Array,String,Function等的prototype 进行扩展。
当然对javascript内置对象的prototype 扩展有两种对立的思路,一种认为这种做法污染了原生的javascript,容易造成版本控制混乱,倾向于用别的方法实现,代表就是jQuery,另一种是认为合理的扩展是必须的,简化API,让程序更好理解,代表是Mootools.
if(typeof id == 'undefined'){
alert('must a string');
}
if(this == window){
return new $(id);
}
this.dom = document.getElementById(id);
}$.prototype = {
test : function(text){
this.dom.innerText = text;
}
}
function $(id){
if(typeof id == 'undefined'){
alert('must a string');
}
return new $.prototype.init(id);
}$.prototype = {
test : function(text){
this.dom.innerText = text;
},
init : function(id){
this.dom = document.getElementById(id);
}
}
$.prototype.init.prototype = $.prototype;
return new $(id);
把自己作为对象返回该如何理解? 在用$().test()的时候 相当于(new $(id)).test()? 然后又返回一个对象(new (new $(id))).test()? 不太明白
return new $(id);
把自己作为对象返回该如何理解? 在用$().test()的时候 相当于(new $(id)).test()? 然后又返回一个对象(new (new $(id))).test()? 不太明白if在那摆着,哪能让你new两次啊。
伦家$的意思是,你要是调用我的时候,用的是$(id)这种形式,就给你修正一下,论家本来要用new $(id)。不排除某些人很懒,忘记加new。so,给你来了个if,然后给你加上new。如果你就是用的new,那if根本进不去。
return new $(id);
把自己作为对象返回该如何理解? 在用$().test()的时候 相当于(new $(id)).test()? 然后又返回一个对象(new (new $(id))).test()? 不太明白if在那摆着,哪能让你new两次啊。
伦家$的意思是,你要是调用我的时候,用的是$(id)这种形式,就给你修正一下,论家本来要用new $(id)。不排除某些人很懒,忘记加new。so,给你来了个if,然后给你加上new。如果你就是用的new,那if根本进不去。
然后呢? new完的对象返回给谁了? test里面用的是dom属性,根本没用到那个对象阿。
function $(id){
this.dom = document.getElementById(id)
}$.prototype = {
changeText : function(){
this.dom.innerText = 'fuck ?';
return this;
},
changeStyle : function(){
this.dom.style.color = 'red';
return this;
}
}
new $('asd').changeText().changeStyle();
</script>
var divDrag = {
o: null,
init: function (divHeader, DivContent) {
divHeader.onmousedown = this.start;
DivContent.onmousedown = this.start;
},
start: function (e) {
var o;
divDrag.o = o = document.getElementById("tanchu_warrper");
e = divDrag.fixEvent(e);
e.preventDefault && e.preventDefault();
o.x = e.clientX - divDrag.o.offsetLeft + 239;
o.y = e.clientY - divDrag.o.offsetTop + 150;
document.onmousemove = divDrag.move;
document.onmouseup = divDrag.end;
},
move: function (e) {
e = divDrag.fixEvent(e);
var oLeft, oTop;
oLeft = e.clientX - divDrag.o.x + 239;
oTop = e.clientY - divDrag.o.y + 150;
divDrag.o.style.left = oLeft + 'px';
divDrag.o.style.top = oTop + 'px';
divleft = oLeft * 1;
divtop = oTop * 1;
},
end: function (e) {
e = divDrag.fixEvent(e);
divDrag.o = document.onmousemove = document.onmouseup = null;
},
fixEvent: function (e) {
if (!e) {
e = window.event;
e.target = e.srcElement;
e.layerX = e.offsetX;
e.layerY = e.offsetY;
}
return e;
}
}
divDrag.init(div, divBox); = -= 像这种 封装方法就可以实现
if(typeof id == 'undefined'){
alert('must a string');
}
if(this == window){
return new $(id);
}
this.dom = document.getElementById(id);
}
$.prototype = {
test : function(text){
this.dom.innerText = text;
}
}
你只需把 $(id)
当做 document.getElementById(id) 的缩写
就可以了
于是 o = $(id) 后 o 就具有了 id 为 'id' 的 dom 对象的一切属性和方法了
function $(id){
if(typeof id == 'undefined'){
alert('must a string');
}
if(this == window){
return new $(id);
}
this.dom = document.getElementById(id);
}$.prototype = {
test : function(text){
this.dom.innerText = text;
}
}但是我的代码里 不返回对象,却不能实现$(id).test(str)这种,只能这样用$.test(str),但是这样就没法用$()获得要响应事件的标签,只能又去写document......了。 总之单独使用$()可以,连用只能$.test()。(function(window){
_$ = window.$;
$ = function(o){
if(Boolean(o) == false)
return event.srcElement;
else
return typeof(o) == "string"?document.getElementById(o):o;
};
$.test=function(str){
document.write('郁闷蛋疼'+str);
}
}(window))
function $(id){
if(typeof id == 'undefined'){
alert('must a string');
}
if(this == window){
return new $(id);
}
this.dom = document.getElementById(id);
}$.prototype = {
test : function(text){
this.dom.innerText = text;
}
}但是我的代码里 不返回对象,却不能实现$(id).test(str)这种,只能这样用$.test(str),但是这样就没法用$()获得要响应事件的标签,只能又去写document......了。 总之单独使用$()可以,连用只能$.test()。(function(window){
_$ = window.$;
$ = function(o){
if(Boolean(o) == false)
return event.srcElement;
else
return typeof(o) == "string"?document.getElementById(o):o;
};
$.test=function(str){
document.write('郁闷蛋疼'+str);
}
}(window))
LZ要先了解下怎么创建对象,你的这个实例不是对象,你的$和$.test只是两个独立的函数,只不过函数名使用了命名空间而已。 可能大家给的例子有点复杂,看看下面function Class(id)
{
//对象的构造函数
}
Class.prototype.mt = function ()
{
//对象的一个方法
alert(34);
}//这个Class对象的使用方法有
//方法1:
var obj = new Class(123);
obj.mt();//方法2:
new Class(123).mt();//方法3:
var $ = function (id)
{
return new Class(id);
};
$(123).mt();方法3就是你想要有吧
function $(id){
if(typeof id == 'undefined'){
alert('must a string');
}
if(this == window){
return new $(id);
}
this.dom = document.getElementById(id);
}$.prototype = {
test : function(text){
this.dom.innerText = text;
}
}但是我的代码里 不返回对象,却不能实现$(id).test(str)这种,只能这样用$.test(str),但是这样就没法用$()获得要响应事件的标签,只能又去写document......了。 总之单独使用$()可以,连用只能$.test()。(function(window){
_$ = window.$;
$ = function(o){
if(Boolean(o) == false)
return event.srcElement;
else
return typeof(o) == "string"?document.getElementById(o):o;
};
$.test=function(str){
document.write('郁闷蛋疼'+str);
}
}(window))
LZ要先了解下怎么创建对象,你的这个实例不是对象,你的$和$.test只是两个独立的函数,只不过函数名使用了命名空间而已。 可能大家给的例子有点复杂,看看下面function Class(id)
{
//对象的构造函数
}
Class.prototype.mt = function ()
{
//对象的一个方法
alert(34);
}//这个Class对象的使用方法有
//方法1:
var obj = new Class(123);
obj.mt();//方法2:
new Class(123).mt();//方法3:
var $ = function (id)
{
return new Class(id);
};
$(123).mt();方法3就是你想要有吧
这回明白了
非常感谢
Class.prototype 是给class添加属性的吧
class.prototype={}