相册管理,主要是选择器的正确使用和before(),after()以及remove()的用法,实现了相册的添加,前移,后移以及删除功能。
实例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>相册管理</title> <link rel="stylesheet" type="text/css" href="css/style.css"> <script type="text/javascript" src="js/jquery.js"></script> <script type="text/javascript" src="js/demo.js"></script> </head> <body> <div class="wrap"> <div class="header"> <h2>相册管理</h2> <p> <label for="img_url">路径:</label> <input type="text" name="img_url" id="img_url" value ="images/jsy.jpg"> </p> <p>类型 : <input type="radio" id="rect" name="border" value="0" checked><label for="rect">直角</label> <input type="radio" id="radius" name="border" value="10%"><label for="radius">圆角</label> <input type="radio" id="circle" name="border" value="50%"><label for="circle">圆型</label> </p> <p>阴影: <select name="shadow"> <option value="0" selected>不添加</option> <option value="1">添加</option> </select> </p> <p><button class="add">添加图片</button></p> </div> <div class="main"> <ul></ul> </div> </div> </body> </html>
运行实例 »
点击 "运行实例" 按钮查看在线实例
实例
<script> $(document).ready(function(){ $('button.add').on('click',function(){ var img_url = $('#img_url').val() if (img_url.length==0) { alert('请选择一张图片') $('#img_url').focus() return false } var img_type = $(':radio:checked').val() var shadow = 'none' if ($(':selected').val() == 1) { shadow = '3px 3px 3px #666' } var img = $('<img>') .prop('src',img_url) .width(150) .height(150) .css({ 'border-radius': img_type, 'box-shadow': shadow }) var before = $('<button>').text('before') var after = $('<button>').text('after') var remove = $('<button>').text('delete') var li = $('<li>').append(img,before,after,remove) li.appendTo('ul') before.click(function() { $(this).parent().prev().before($(this).parent()) }); after.click(function() { $(this).parent().next().after($(this).parent()) }); remove.click(function() { $(this).parent().remove() }); }) }) </script>
运行实例 »
点击 "运行实例" 按钮查看在线实例
实例
.wrap { width: 360px; height: auto; background-color: lightgray; border: 1px solid #cecece; color: #363636; border-radius: 5px; } .wrap .header { padding: 15px; text-align: center; } .wrap .header h2 { text-align: center; } .add { width: 100px; height: 30px; border: none; cursor: pointer; background-color: #1E90FF; border-radius: 3px; color: white; } .add:hover { opacity: 0.8; } .main { overflow: hidden; } .main ul { padding: 0; margin: 0; } .main ul li { list-style-type: none; float: left; margin-left: 20px; margin-bottom: 10px; width: 150px; height: 200px; text-align: center; border-radius: 3px; } .main ul li button { color: #fff; margin:3px; border: none; border-radius: 3px; background-color: #1E90FF;; } .main ul li button:hover { background-color: orange; color:white; cursor: pointer; }
运行实例 »
点击 "运行实例" 按钮查看在线实例
2.ajax的post请求
实例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Ajax</title> <style type="text/css"> form{ width: 300px; margin: auto; } </style> </head> <body> <form action="login.php" method="post"> <fieldset> <legend >login</legend> <p> <label for="email">email:</label> <input type="email" id="email"> </p> <p> <label for="psd">passd:</label> <input type="password" id="psd"> </p> <p> <button>login</button> <span id="tips" style="font-size:1.2em;font-weight: bolder;color:red"></span> </p> </fieldset> </form> </body> </html> <script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script> <script type="text/javascript"> $('button:first').click (function(){ $.post( 'login.php', { "email":$('#email').val(), "psd":$('#psd').val() }, function(res){ if(res == 1 ){ $('#tips').text('success ...') setTimeout(function(){ location.href = 'index.php' },1500) }else{ $('#tips').text('fail ...') $('#email').focus() setTimeout("$('#tips').empty()",1500) } },'json' ) return false }) </script>
运行实例 »
点击 "运行实例" 按钮查看在线实例
3.手写
总结:$.post(url,data,success,dataType)使用一个HTTP POST请求从服务器加载数据,其中url为post请求地址,data是发送的数据,success是请求成功后执行的回调函数,dataType是数据的格式,一般为json或xml格式,