首页 > 代码库 > jquery
jquery
$(function () {
$(‘.out‘).click(function () {
$(‘#box‘).fadeOut(‘slow‘);
});
$(‘.in‘).click(function () {
$(‘#box‘).fadeIn(‘slow‘);
});
$(‘.toggle‘).click(function () {
$(‘#box‘).fadeToggle(‘slow‘);
});
$(‘.to‘).click(function () {
$(‘#box‘).fadeTo(‘slow‘, 0.33); //0-1之间
});
<input type="button" class="in" value="http://www.mamicode.com/淡入" />
<input type="button" class="out" value="http://www.mamicode.com/淡出" />
<input type="button" class="toggle" value="http://www.mamicode.com/切换" />
<input type="button" class="to" value="http://www.mamicode.com/透明度到" />
<div id="box">
box
</div>
$(function () {
/*
//异步加载数据
$(‘input‘).click(function () {
$(‘#box‘).load(‘test.html .url‘);
});
$(‘input‘).click(function () {
$(‘#box‘).load(‘test.php‘);
});
$(‘input‘).click(function () {
$(‘#box‘).load(‘test.php?url=ycku‘);
});
$(‘input‘).click(function () {
$(‘#box‘).load(‘test.php‘, {
url : ‘ycku‘
});
});
*/
$(‘input‘).click(function () {
$(‘#box‘).load(‘test.php‘, {
url : ‘ycku‘
}, function (response, status, xhr) {
//alert(response);
//$(‘#box‘).html(response +‘123‘);
//if (status == ‘success‘) {alert(‘成功后的处理‘);}
//alert(xhr.responseText);
//alert(xhr.responseXML);
//alert(xhr.status);
alert(xhr.statusText);
});
});
});
index.html
<input type="button" value="http://www.mamicode.com/异步加载数据" />
<div id="box"></div>
index.php
<?php
/*
if ($_GET[‘url‘] == ‘ycku‘) {
echo ‘瓢城Web俱乐部‘;
} else {
echo ‘木有!‘;
}
*/
if ($_POST[‘url‘] == ‘ycku‘) {
echo ‘瓢城Web俱乐部‘;
} else {
echo ‘木有!‘;
}
?>
$(function () {
/*
$(‘input‘).click(function () {
$.get(‘test.php?url=ycku‘, function (response, status, xhr) {
$(‘#box‘).html(response);
});
});
//通过直接在url问号紧跟传参
$(‘input‘).click(function () {
$.get(‘test.php‘, ‘url=ycku‘,function (response, status, xhr) {
$(‘#box‘).html(response);
});
});
//通过第二个参数data,字符串形式的键值对传参,然后自动转换为问号紧跟传参
$(‘input‘).click(function () {
$.get(‘test.php‘, {
url : ‘ycku‘
},function (response, status, xhr) {
$(‘#box‘).html(response);
});
});
//通过第二个参数data,对象形式的键值对传参,然后自动转换为问号紧跟传参
$(‘input‘).click(function () {
$.post(‘test.php?url=ycku‘, function (response, status, xhr) {
$(‘#box‘).html(response);
});
});
//post提交不能使用问号传参
$(‘input‘).click(function () {
$.post(‘test.php‘, ‘url=ycku‘,function (response, status, xhr) {
$(‘#box‘).html(response);
});
});
//post提交可以使用字符串形式的键值对传参,自动转换为http消息实体传参
$(‘input‘).click(function () {
$.post(‘test.php‘, {
url : ‘ycku‘
},function (response, status, xhr) {
$(‘#box‘).html(response);
});
});
//post提交可以使用对象键值对
$(‘input‘).click(function () {
$.post(‘test.php‘, {
url : ‘ycku‘
},function (response, status, xhr) {
$(‘#box‘).html(response);
}, ‘html‘); //PHP文件返回的数据是纯文本,默认是html或text
});
$(‘input‘).click(function () {
$.post(‘test.php‘, {
url : ‘ycku‘
},function (response, status, xhr) {
$(‘#box‘).html(response);
}, ‘json‘);
});
//本身是纯文本,如果强行按照xml或者json数据格式返回的话,那么就无法获取数据
$(‘input‘).click(function () {
$.post(‘test.xml‘,function (response, status, xhr) {
alert(response);
}, ‘html‘); //默认type就已经是xml
});
//如果默认已经是xml,强行设置为html,则会连xml标签也返回
$(‘input‘).click(function () {
$.post(‘test.xml‘,function (response, status, xhr) {
alert($(response).find(‘root‘).find(‘url‘).text());
});
});
$(‘input‘).click(function () {
$.post(‘test.json‘,function (response, status, xhr) {
alert(response[0].url);
});
});
$(‘input‘).click(function () {
$.getJSON(‘test.json‘,function (response, status, xhr) {
alert(response[0].url);
});
});
*/
$(‘input‘).click(function () {
$.getScript(‘test.js‘);
});
});