87,992
社区成员
发帖
与我相关
我的任务
分享
<!doctype html>
<html>
<head>
<meta charset="gb2312" />
<title></title>
<style>
</style>
</head>
<body>
<div id="a">a</div>
<div id="b">b</div>
<script>
(function(){
var j = function(o){
return j.fn.init(o);
};
j.fn = j.prototype = {
init: function(o){
this[0] = document.getElementById(o);
this.length = 0;
return this
},
html: function(str){
if(str!=null){
this[0].innerHTML = str;
return this
}
return this[0].innerHTML;
},
size:function(){
return this.length;
}
}
window.$ = j;
})()
//alert( $('a').html() ) //a 框架成功
var b = $('b').html()
//$('a').html( b ) // 这样能够赋值成功
$('a').html( $('b').html() ) //但是 我不想用变量过渡,怎么用这样方式。
//我看了一下 jquery中 用isFunction来判断传来的变量,具体怎么实现的,不懂。
// 也就是说 怎么判断 html( str ) 中 str是否为函数,如果是函数,先运行它,再给innerHTML 赋值
</script>
</body>
</html>
(function(){
var j = function(o){
return j.fn.init(o);
};
j.isFunction = function(f){
return typeof f == 'function' || f instanceof Function;
};
j.fn = j.prototype = {
init: function(o){
this[0] = document.getElementById(o);
this.length = 0;
return this
},
html: function(str){
if(typeof str == 'string'){
this[0].innerHTML = str;
return this
}
if(j.isFunction(str)){
// 不知你要判断它是函数做甚么,没有任何意义,html顾名思义,就是获取或设置html的方法,传个函数进来作甚?
// 如果是要判断传入的是j对象或j本身,可以用 str instanceof j 或 str === j
}
return this[0].innerHTML;
},
size:function(){
return this.length;
}
}
window.$ = j;
})()
(function(){
var j = function(o){
return j.fn.init(o);
};
j.isFunction = function(f){
return typeof f == 'function' || f instanceof Function;
};
j.fn = j.prototype = {
init: function(o){
this[0] = document.getElementById(o);
this.length = 0;
return this
},
html: function(str){
if(typeof str == 'string'){
this[0].innerHTML = str;
return this
}
if(j.isFunction(str)){
// 不知你要判断它是函数做甚么,没有任何意义,html顾名思义,就是获取或设置html的方法,传个函数进来作甚?
// 如果是要判断传入的是j对象或j本身,可以用 str instanceof j 或 str === j
}
return this[0].innerHTML;
},
size:function(){
return this.length;
}
}
window.$ = j;
})()
var j = function(o){
return j.fn.init(o);
};// step 1:先将有 j.fn = j.prototype = {...}
// step 2:将j.fn赋值给j.fn.init的原型链,为的就是new一个j对象,因为直接new j()会执行j中的代码,不易操作
j.fn.init.prototype = j.fn;
var j = function(o){
// step 3:现在new j.fn.init 同等于 new j,但是不会执行到j()函数里面的代码,同时具备j.prototype原形链上的所有属性
return new j.fn.init(o);
};
刚刚接触jquery 对jquery 的学习还没有达到
这张程度, 来学习的 帮你顶下
(function(){
var j = function(o){
return new j.fn.init(o); //////
};
j.fn = j.prototype = {
init: function(o){
this[0] = document.getElementById(o);
this.length = 1;
return this
},
html: function(str){
if(str != null){
this[0].innerHTML = str;
return this
}
return this[0].innerHTML;
},
size:function(){
return this.length;
}
}
j.fn.init.prototype = j.fn; ///////
window.$ = j;
})()
$('a').html($('b').html());
<!doctype html>
<html>
<head>
<meta charset="gb2312" />
<title></title>
<style>
</style>
</head>
<body>
<div id="a">i am a</div>
<div id="b">i am b</div>
<script>
(function(){
var j = function(o){
return j.fn.init(o);
};
j.fn = j.prototype = {
init: function(o){
this[0] = document.getElementById(o);
this.length = 0;
return this
},
html: function(str){
if(str!=null){
this[0].innerHTML = str;
return this
}
return this[0].innerHTML;
},
size:function(){
return this.length;
}
}
window.$ = j;
})()
//我的意思是
//比如现在我们想把 a 的innerHTML变为 b 的innerHTML
//方法1
var b = $('b').html()
//$('a').html( b ) //这里需要一个变量过渡
//方法2
//不用变量,直接 -->
$('a').html( $('b').html() )
/*
html: function(str){
if(str!=null){
this[0].innerHTML = str;
return this
}
return this[0].innerHTML;
}
str有值(直接值)时,可以赋值成功
但是 str为 $('b').html() 这样的方式时,却赋值不成功,
我的问题是,怎么让 $('a').html( $('b').html() ) 这样不使用变量过渡的方式也能成功赋值
*/
</script>
</body>
</html>