Home > Article > Web Front-end > Detailed explanation of jquery load method usage
jquery load is a function in jquery ajax. Load can conveniently and quickly load a page directly into the specified p (html, php), and it can take parameters. Let me tell you Detailed introduction to the usage of the load method.
Format
load(url,data,function(response,status,xhr))
How to use data
1. Load a php file that does not contain passing parameters
The code is as follows | Copy code |
$("#myID").load("test.php"); |
//Import the result after running test.php in the element with ID #myID
2. Load a php file that contains a passing parameter
The code is as follows | Copy code |
$("#myID").load("test.php",{"name" : " Adam"}); //The imported php file contains a passing parameter, similar to: test.php?name=Adam or directly $("# pResult").load("jqueryLoad?username=" + username + "&un="+$("#username").val()+"×tamp=" + (new Date()).getTime()); |
When sending parameters, the parameters must be encoded twice:
The code is as follows | Copy code |
var username = encodeURI(encodeURI($("#username").val())); |
Use AJAX request to change the text of p element:
The code is as follows | Copy code |
$("button").click(function(){ |
How to use callback
For example, if we want to slowly display the loaded content after the load method gets the server response, just You can use the callback function. The code is as follows:
The code is as follows | Copy code |
$("#go") .click(function(){ $("#myID").load("welcome.php", {"lname" : "Cai", "fname" : "Adam", function(){ $("#myID").fadeIn('slow');} ); }); |
例子
代码如下 | 复制代码 |
jQuery load方法 演示姓: $("#go").click(function(){
|
Note: Only if the load processing function is bound before this element is completely loaded, it will be triggered after it is loaded. If you bind it later, it will never trigger. So don't bind the load event in $(document).ready(), because jQuery will bind the load event after all DOM loading is completed
The above is the detailed content of Detailed explanation of jquery load method usage. For more information, please follow other related articles on the PHP Chinese website!