引入JQuery <script src="jquery.js"></script>在官网http://jquery.com/可下载到最新版本,Production版本是压缩过的代码,文件小,但是不方便阅读。Development是开发版的代码,文件较大,可读性较好。
<script type="text/javascript">
$(document).ready(function(){
//code goes here
});
</script>
$的意思是jQuery = window.jQuery = window.$=$,因此JQuery(document)=$(document) document是网页中的对象,这里指代整个网页内容。
获取一个class的方法jQuery(".someClass").css("border", "1px solid red")
获取一个id的方法jQuery("#someId").html("<b>So Bold!</b>")
$(document).ready(function(){
//code goes here
});
相当于window.onload = function() 但是前者只要As soon as the DOM (Document Object Model) is ready, the code inside of the ready(function(){}) will start.
添加一个mouseover和mouseout事件:$('a.affLink').mouseover(function(){window.status=this.title;return true;})
.mouseout(function(){window.status='Done';return true;});
等同于
$('a.affLink').mouseover(function(){window.status=this.title;return true;});
$('a.affLink').mouseout(function(){window.status='Done';return true;});
<script src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('a.affLink').mouseover(function(){window.status=this.title;return true;})
.mouseout(function(){window.status='Done';return true;});
});
</script>