JQuery基础语法小结
1、$(document)将document对象转换为jquery
$(document).ready(function(){ alert("helloworld"); });
2、获取所有的超链接对象,并且增加onclick事件;其实在底层jquery对象获得的每个标签的数组,因此我们不需要进行循环了
$(document).ready(function(){ $("a").click(function(){ alert("helloworld"); }); });
3、jquery对象与dom对象之间的转换
$(document).ready(function(){ varjavascriptElement=document.getElementById("clickme"); //将dom对象转换为jquery对象 varjqueryElement=$(javascriptElement); //alert("javascript:"+javascriptElement.innerHTML);//innerHTML获取标签的内容 //alert("jquery:"+jqueryElement.html()); //将jquery转换为dom对象方式一 varjElement=$("#clickme"); varjavascriptEle=jElement[0]; alert("jquery1:"+javascriptEle.innerHTML); //方式二 varjavascriptEle2=jElement.get(0); alert("jquery2:"+javascriptEle2.innerHTML); });
4、jquery解决id值是否存在的问题
<aid="hello">clickme</a> <scripttype="text/javascript"> //以传统方式解决id为空方式一 if(document.getElementById("helllo")){ //如果hello不存在会出错 document.getElementById("helllo").style.color="red"; } //方式二 //$("#hello")[0].style.color="red"; //用jquery解决 $("#hello").css("color","red"); //如果只有一个参数,则此方法是读,如果是两个参数则是些功能(css(“”),css(“”,“”)) alert($("#hello").css("color")); </script>
5、在javascript中规定,你要操作css属性时要将属性中的‘-'去掉,同时把后一个字母大写;如:background-color要改成backgroundColor
6、jquery中也使用选择器来操纵各种各样的元素。是继承了css的设计理念,但是把它更是发扬光大了;
7、jquery的几种书写形式
1>方式一
$("document").ready(function(){ ... });
2>方式二
$().ready(function(){ ... });
3>方式三
$(function(){ ... });
以上几种方式是一样的
希望本文所述对大家学习jquery程序设计能有所帮助。