jQuery常用代码片段

jQuery处理JSON
function parseJson(){
	//Start by calling the json object, I will be using a 
        //file from my own site for the tutorial 
	//Then we declare a callback function to process the data
	$.getJSON('hcj.json',getPosts);
 
	//The process function, I am going to get the title, 
        //url and excerpt for 5 latest posts
	function getPosts(data){
 
		//Start a for loop with a limit of 5 
		for(var i = 0; i < 5; i++){
			//Build a template string of 
                        //the post title, url and excerpt
			var strPost = '<h2>' 
				      + data.posts[i].title
				      + '</h2>'
				      + '<p>'
				      + data.posts[i].excerpt
				      + '</p>'
				      + '<a href="'
				      + data.posts[i].url
				      + '" title="Read '
				      + data.posts[i].title
				      + '">Read ></a>';
 
			//Append the body with the string
			$('body').append(strPost);
 
		}
	}
 
}
 
//Fire off the function in your document ready
$(document).ready(function(){
	parseJson();				   
});



jQuery实现让整个div可以被点击
$(".myBox").click(function(){      window.location=$(this).find("a").attr("href");       return false; });

最好的办法是把<script>标签放到HTML文档的最后,</body>标签之前:
<html>
<head>
  <meta charset="utf-8">
  <title>Example</title>
</head>
<body>
  <script src="file.js"></scirpt>
</body>
</html>

你可能感兴趣的:(jquery)