Correction status:Uncorrected
Teacher's comments:
获取图片的url地址用到split()方法进行分割
三个功能按钮需要获取当前元素节点的父节点元素‘li’进行操作
前移和后移,需要获取到前一个或后一个节点进行移动
jQuery中的prev()方法对应着原生js的previousElementSibling属性
next()方法对应着--》nextElementSibling
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>使用jQuery使用动态相册管理,2019.05.21示例</title> <style> .warp { width: 360px; height: auto; background-color: linen; border: 3px double grey; color: #363636; border-radius: 2%; box-shadow: 2px 2px 2px #888; margin: 0 auto; } .warp .header { padding: 15px; } .warp .header h2 { text-align: center; } .add { width: 100px; height: 30px; border: none; cursor: pointer; background-color: deepskyblue; color: white; } .add:hover { background-color: orange; font-size: 1.1rem; } .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; } .main ul li button { margin: 3px; border: none; border-radius: 5%; background-color: deepskyblue; } .main ul li button:hover { background-color: orange; color: white; cursor: pointer; } </style> </head> <body> <div class="warp"> <div class="header"> <h2>私人相册</h2> <p> <label for="img_url">输入图片地址</label> <input type="file" name="img_url" id="img_url" placeholder="图片地址"> </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> <label for="shadow">是否添加阴影:</label> <select name="shadow" id="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> <script src="static/js/jQuery-3.4.1.js"></script> <script> $(function(){ //按钮添加事件 $('button.add').on('click',function () { //1.获取图片基本信息 var imgUrl = $('#img_url').val(); if(imgUrl.length === 0){ alert('请选择一张图片'); $('#img_url').focus(); return false; } var imgType = $('input[type="radio"]:checked').val(); var shadow = 'none'; if($(':selected').val() === '1'){ shadow = '2px 2px 2px #888'; } console.log(shadow); //转换图片url var realImgUrl = imgUrl.split('\\')[2]; imgUrl = 'http://yuanyi.cn/0521/static/images/'+realImgUrl; //console.log(imgUrl); //2.创建img并添加到一个li中 var img = $('<img>').attr({ src:imgUrl, width:150, height:150 }).css({ 'border-radius':imgType, 'box-shadow':shadow }); var before = $('<button></button>').text('前进'); var after = $('<button></button>').text('后移'); var remove = $('<button></button>').text('删除'); //添加到ul中 var container = $('<li>'); container.append(img,before,after,remove); container.appendTo('ul'); //三个功能按钮 //前移 before.on('click',function () { var current = $(this).parent(); //console.log(current); var prev = current.prev(); prev.before(current); }); //后退 after.on('click',function () { var current = $(this).parent(); var next = current.next(); next.after(current); }); //删除 remove.on('click',function () { return confirm('是否删除') ? $(this).parent().remove() : false; }); }); }); </script> </body> </html>
点击 "运行实例" 按钮查看在线实例