How to create our own function and how to call in the Ajax in JQUERY?
Hi I want to create my own function using jquery.And I want to call that function in t开发者_运维知识库he Ajax.and the calling function should not be like $('div').myfunctionname();
It is not so complicated here is the tutorial from Jquery docs
The format to create a plugin/function is this
(function( $ ){
$.fn.myPlugin = function() {
// Do your awesome plugin stuff here
};
})( jQuery );
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>User Function Ajax with Jquery </TITLE>
<SCRIPT src='jquery-1.4.js' LANGUAGE="JavaScript">
</SCRIPT>
<SCRIPT LANGUAGE="JavaScript">
$(document).ready(function(){
$.fn.changecontent = function(result) {
alert(result);
$(result).find('record').each(function(){
var from=$(this).find('from').text();
var to=$(this).find('to').text();
var sub=$(this).find('sub').text();
$('div#loading').append('<table border="2"><tr><td>'+from+'</td></tr><tr><td>'+to+'</td></tr><tr><td>'+sub+'</td></tr></table>');
});
};
$.ajax({url:"Ajax.xml", success:function(result){
$().changecontent(result);
}});
});
</SCRIPT>
</HEAD>
<BODY>
<div id='loading'/>
</BODY>
</HTML>
精彩评论