看看Jquery,再看看传统的JS,我们来效仿一$(function(){$(“#a”).click=function(){};});吧
2011-06-03 12:05
363 查看
Jquery简化了传统JS实现功能的代码量,例如:
jquery的$(function(){});相当于传统JS的window.onload=function(){};
还是看一下我写的代码吧:
[code]window.onload=function(){
//varnewTextNode=document.createTextNode("<imgsrc='"+Zhang.init.src
//+"'width="+Zhang.Width+"height="+Zhang.Height+">");//添加文本
[/code]
jquery的$(function(){});相当于传统JS的window.onload=function(){};
还是看一下我写的代码吧:
<scripttype="text/javascript">
[code]window.onload=function(){
//定义一个类对象
varZhang={
Title:"",
Width:"100",
Height:"",
Zhang:function(title,width,height){
this.Title=title;
this.Width=width;
this.Height=height;
},
Zhang:function(title,height){//方法的重载
this.Title=title;
this.Height=height;
},
init:{target:"_blank",src:""},
generate:function(){
varinsertedNode=document.createElement('img');//添加HTML標記
insertedNode.setAttribute('src',Zhang.init.src);
insertedNode.setAttribute("width",Zhang.Width);
insertedNode.setAttribute("height",Zhang.Height);
//varnewTextNode=document.createTextNode("<imgsrc='"+Zhang.init.src
//+"'width="+Zhang.Width+"height="+Zhang.Height+">");//添加文本
document.getElementsByTagName("body")[0].appendChild(insertedNode);
}
}
varobj=document.getElementById("add");
Zhang.Zhang("图像测试","300","600");//覆盖原来的值100
Zhang.init.src="http://d4.sina.com.cn/201106/03/312669_78090-biaoxian.JPG";
if(typeof(obj.addEventListener)!="undefined"){//判断浏览器
obj.addEventListener("click",Zhang.generate,true);//触发单击事件
}else{
obj.attachEvent("onclick",Zhang.generate);//绑定方法
}
};
</script>
[/code]
相关文章推荐
- ajaxfileupload.js结合低版本jquery报异常:TypeError: jQuery.handleError is not a function
- ajaxfileupload.js jQuery.handleError is not a function
- JS 实现 jQuery的$(function(){});
- jquery bind event, use on. $(document).on("click","#a",function(){alert(1)}) [#document]
- 关于js中的onclick事件和JQuery中的click方法
- AngularJS vs. jQuery,看看谁更胜一筹
- jquery.form.js关于$("form").ajaxSubmit is not a function原因 .
- jquery-ui.min.js:5 Uncaught TypeError: b.nodeName.toLowerCase is not a function
- Jquery中的事件绑定$("#btn").bind("click",function(){ })
- 重写jquery.confirm.js 摆脱传统alert confirm 插件供下载(四)
- 原生js模拟jQuery对象调用click事件来理解this和$(this)
- JS传统的节点创建和JQuery的比较
- jquery开始——我们来看看地图
- 今天我们来分享10款最新的jQuery和HTML5应用插件,都非常强大,一起来看看吧。
- 重写jquery.confirm.js 摆脱传统alert confirm 插件供下载(三)
- screen,client,page三种确定鼠标坐标的区别和原生JS事件写法,区别于Jquery的$.on(x,y);和$.click()
- ajaxfileupload.js上传文件时jQuery.handleError is not a function的 解决办法
- js $(document).ready()、()(jQuery)、window.onload=function(){} 区别
- jquery阻止冒泡事件:$('span').bind("click",function(event){event.stopPropagation();})(有用源)
- AngularJS vs. jQuery,看看谁更胜一筹