以下代碼是使用jQuery的的知識(shí),根據(jù)課堂案例編寫的武林高手在線相冊(cè)的案例:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>武林高手在線相冊(cè)</title> <style type="text/css"> .warp { width: 360px; height: auto; font-family: 楷體; font-size: 1rem; background-color: #efefef; border: 3px double grey; color: #363636; border-radius: 2%; margin: auto; } input , select { font-family: 楷體; font-size: 0.8rem; } .warp .header { padding: 15px; } .warp .header h2 { text-align: center; } .add { width: 100px; height: 40px; font-family: 楷體; background-color: lightblue; color: white; font-size: 1rem; } .add:hover { font-family: 楷體; background-color: pink; color: black; border-radius: 5%; cursor: pointer; } .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-radius: 5%; background-color: lightblue; } .main ul li button:hover { cursor: pointer; background-color: pink; color: white; } </style> </head> <body> <div class="warp"> <div class="header"> <h2>武林高手在線相冊(cè)</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"><label>直角</label> <input type="radio" id="rect" name="border" value="10%"><label>圓角</label> <input type="radio" id="rect" name="border" value="50%" checked><label>圓形</label> </p> <p> 是否添加陰影: <select name="shadow"> <option value="0">不添加</option> <option value="1" selected>添加</option> </select> </p> <p><button class="add">添加圖片</button></p> </div> <div class="main"> <ul></ul> </div> </div> <script src="../lib/jquery.js"></script> <script> //分三步完成 $(function (){ $('button.add').on('click',function(){ //1.獲取圖片的相關(guān)信息 //判斷用戶是否選擇了圖片 let img_url = $('#img_url').val(); if (img_url.length === 0){ alert('請(qǐng)選擇一張圖片'); $('#img_url').focus(); return false; } //獲取圖片的基本特征 //獲取圖片的外觀 let img_type = $(':radio:checked').val(); //是否添加陰影 let shadow = 'none'; if ($(':selected').val() ==='1'){ shadow = '3px 3px 3px #666'; } //2.創(chuàng)建圖片并添加到頁(yè)面中 img_url = 'http://www.whj.com/0918/images/'+img_url.split('\\')[2]; //創(chuàng)建一張圖片 let img = $('<img>') .attr('src',img_url) .width(150) .height(150) .css({ 'border-radius':img_type, 'box-shadow':shadow }); //創(chuàng)建三個(gè)按鈕 let before = $('<button></button>').text('前移'); let after = $('<button></button>').text('后移'); let remove = $('<button></button>').text('刪除'); //創(chuàng)建一個(gè)<li>用來存放所有的內(nèi)容 let contaier = $('<li>'); //將圖片和按鈕打包到<ul>中 contaier.append(img,before,after,remove); //將<li>標(biāo)簽添加到<ul>中 contaier.appendTo('ul'); //為三個(gè)操作填加功能 //前移功能 before.click(function (){ //第一步獲取要移動(dòng)的圖片 let nowpic = $(this).parent(); let prev = nowpic.prev(); prev.before(nowpic); }); //后移功能 after.click(function (){ //第一步獲取要移動(dòng)的圖片 let nowpic = $(this).parent(); let next = nowpic.next(); next.after(nowpic); }); //刪除 remove.click(function (){ if(confirm('確認(rèn)刪除?')){ $(this).parent().remove(); } return false; }) }) }); </script> </body> </html>
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
說明:此案例就是簡(jiǎn)單實(shí)現(xiàn)了一些jQuery的一些操作,共分為三步:
獲取圖片的相關(guān)信息:拿到從頁(yè)面中所選擇的圖片的相關(guān)信息
2.創(chuàng)建圖片并添加到頁(yè)面中:這里主要注意的是如何獲取到圖片的路徑
3.創(chuàng)建三個(gè)按鈕并為三個(gè)操作填加功能:這里圖片和是三個(gè)按鈕時(shí)放在同一個(gè)<li>標(biāo)簽中作為一個(gè)整體的。
總結(jié):jQuery中最多的就是一些常用的操作,篩選方式可以分為:
1. 直接從集合中獲取元素:eq(),first(),last();
2. 根據(jù)元素在集合中的關(guān)系:
[1].next(),nextAll(),nextUntil(): 向后查詢
[2].prev(),prevAll(),prevUntil(); 向前查詢
[3].siblings(): 向前和向后進(jìn)行雙向查詢
[4].parent(): 獲取父級(jí)元素
[5].children(),find(),filter(),not():多級(jí)查詢
[6].is(),has(): 判斷查詢
3. 集合區(qū)間查詢與元素添加: slice(), add()
jQuery中的基本事件為以下幾種:
1.鼠標(biāo)事件: click點(diǎn)擊, mouseenter移入,mouseleave移出;
2.表單事件: submit提交, change內(nèi)容改變,focus獲取焦點(diǎn), blur失去焦點(diǎn)
多用多練才能熟練地學(xué)以致用。
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號(hào)
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號(hào)