Using jquery imitation as an example of microblogging function

  • 2020-03-30 02:40:39
  • OfStack

The source code:
 
<!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> Post box </title> 
<script src="../scripts/jquery-1.3.1.js" type="text/javascript"></script> 
<!-- 
 Function description: when you click the input box, the prompt text will be removed; When the cursor leaves, if nothing is entered, the text of the prompt is restored. If there is input text, the remaining word count is calculated and displayed  
--> 
<script type="text/javascript"> 

$(function(){ 
$("#content").focus(function(){ 
if ($(this).val()==this.defaultValue) { 
$(this).val(""); 
//alert($(this).length-1); 
} 
}).blur(function(){ 
if ($(this).val()=='') { 
$(this).val(this.defaultValue); 
} 
}) 
$("#content").keyup(function(){ 
//alert($(this).val().length); 
var words_num = 140 - $(this).val().length; 
if (words_num < 0) { 
//It's misdirected so there's no return value
//$("font").text(function(words_num){ 
// return "<font color='red'>"+words_num+"</font>"; 
//}); 
$("font").css('color','red').text(words_num); 
}else{ 
$("font").text(words_num); 
//alert(words_num); 
} 
}) 
}); 

$(function(){ 
$("#send").click(function(){ 
$.post("post3.php", { 
// username : $("#username").val() , 
content : $("#content").val() 
}, function (data, textStatus){ 
// var username = data.username; 
var content = data.content; 
// var txtHtml = "<div class='comment'><h6>"+username+":</h6><p class='para'>"+content+"</p></div>"; 
var txtHtml = "<div><h3>"+content+"</h3></div>"; 
$("#resText").html(txtHtml); //Add the returned data to the page
},"json"); 
}) 
}); 
</script> 
</head> 

<body> 
<fieldset style="width:800px; margin-left:300px;"> 
<legend style="font-sixe:16px; font-weight : 600"> Post box </legend> 
<form action="#" id="form1"><!--enctype="multipart/form-data"--> 
 You can also type <span id="num" style="font-size:28px;font-weight:800"><font color="green">140</font></span> A word  
<textarea cols="96" rows="8" id="content"> Just write something down .</textarea> 
<input type="button" id="send" value=" release "/> 
</form> 
</fieldset> 
<div> The message you sent was: </div> 
<div id="resText"> 
</div> 
</body> 
</html> 

Effect:
< img SRC = "border = 0 / / files.jb51.net/file_images/article/201404/201404181139505.gif? 201431811406 ">  
Problems encountered:

Mainly is the problem of jquery library: using jquery-1.3.1.js this file, can achieve the function, but using jquery-1.7.1.min.js this file, there is no effect! I spent a lot of time to source code to find the problem!!

Related articles: