87,923
社区成员
发帖
与我相关
我的任务
分享
<html>
<head>
<style>
.ss {
color: #000000;
font-size: 14px;
background-color: #f3f3f3;
}
</style>
<script type="text/javascript" src="../jquery.js"></script>
<script>
$.fn.extend({
objOver:function() {
this.hover(function() { //XXX
$(this).addClass("ss"); //YYY
}, function() {
$(this).removeClass("ss");
});
}
});
</script>
</head>
<body>
<p>
<input type="text" value = "text1" id="test1">
<input type="button" value="Click2" id="test2">
</p>
</body>
</html>
<script>
$("#test1").objOver();
</script>
<!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" />
<title>无标题文档</title>
<script>
//jQ代码
(function(){
var jQuery = function(s){
return new jQuery.fn.init(s);
}
jQuery.fn = {
init:function(s){
//这里是jq的实际查询方法,不作模拟。
this[0] = s;
},
version:'为了证明我是jQ',
click:function(f){
f.call(this[0]);
return this;
},
extend:function(o){
//这里是简单的模拟的jq的extend方法,当然jq里面的extend要复杂些
for(a in o){
this[a] = o[a];
}
}
}
jQuery.fn.init.prototype = jQuery.fn;
window.$ = jQuery;
})()
//jQ的插件实现机制
$.fn.extend({
hover:function(){
alert('我是hover方法,可以弹出来version:' + this.version);//这里的this是jQ对象,所有有version属性
}
});
//jQ应用
$('这里是选择器').click(function(){
alert('我是click方法。这个时候的this是:' + this);
alert('我是click方法,我没有version属性' + this.version);//这里的this是传进来的字符串,所以不存在version属性,就是undefined
}).hover();
</script>
</head>
<body>
</body>
</html>
<script>
$.fn.extend({
objOver:function() {
this.hover(function() { //XXX
$(this).addClass("ss"); //YYY
}, function() {
$(this).removeClass("ss");
});
}
});
</script>
$.fn.extend({
objOver:function() {
this.hover(function() { //XXX
$(this).addClass("ss"); //YYY
}, function() {
$(this).removeClass("ss");
});
}
});
$.fn.extend({
objOver:function() {
this.hover(function() { //XXX
$(this).addClass("ss"); //YYY
}, function() {
$(this).removeClass("ss");
});
}
});