国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

【原創(chuàng)】Jquery初體驗(yàn)二

2019-11-14 16:10:17
字體:
供稿:網(wǎng)友

快速導(dǎo)航

一.傳統(tǒng)方式生成Table

二.使用jquery.tmpl插件快速生成Table

三.Jquery中的操作class的幾個(gè)方法

四:jq里面的克隆

五:屬性過濾器

六:表單元素過濾器

一.傳統(tǒng)方式生成Table

        var peopleArr = [            { "name": "劉歡", "age": 50, "skill": "從頭再來" },            { "name": "楊坤", "age": 35, "skill": "32唱演唱會(huì)" },            { "name": "那英", "age": 50, "skill": "白天不懂夜的黑" },            { "name": "王菲", "age": 45, "skill": "復(fù)合" }        ]        $(function () {            var $tbCreate = $("<table></table>")//生成table            var $trTitle = $("<tr></tr>").append("<td>序號(hào)</td>").append("<td>姓名</td>").append("<td>年齡</td>").append("<td>技能</td>");            $tbCreate.append($trTitle);//將標(biāo)題加到tb中            //循環(huán)數(shù)組,生成tr            for (var i = 0; i < peopleArr.length; i++) {                //每循環(huán)一次生成一個(gè)tr                var $trCreate = $("<tr><td><input type='checkbox' class='chkOne'/>" + (i + 1) + "</td></tr>");                //循環(huán)對(duì)象數(shù)組,生成其他td                for (var item in peopleArr[i]) {                    var $tdCreate = $("<td>" + peopleArr[i][item] + "</td>");                    //加到tr里面                    $trCreate.append($tdCreate);                }                //將tr加到table里面                $tbCreate.append($trCreate);            }            //將table加到body里面            $(document.body).append($tbCreate);

 

二.使用jquery.tmpl插件快速生成Table

    <script src="jquery/jquery-1.9.1.js"></script>    <script src="jquery/jquery.tmpl.min.js"></script>首先要引入這個(gè)js

需要顯示的字段在這里先占位 <script type="text/x-jquery-tmpl" id="tmpl01"> <!--//準(zhǔn)備模板,使用占位符,屬性名--> <tr> <td><input type="checkbox" class="chkOne" /></td> <td>${name}</td> <td>${age}</td> <td>${skill}</td> <td><a href="#">刪除</a> <a href="#">編輯</a></td> </tr> </script> <script type="text/javascript"> var peopleArr = [ { "name": "劉歡", "age": 50, "skill": "從頭再來" }, { "name": "楊坤", "age": 35, "skill": "32唱演唱會(huì)" }, { "name": "那英", "age": 50, "skill": "白天不懂夜的黑" }, { "name": "王菲", "age": 45, "skill": "復(fù)合" } ] //.tmpl方法,只有導(dǎo)入插件才有 //循環(huán)對(duì)象數(shù)組,數(shù)組里面的每一個(gè)對(duì)象,都會(huì)生成一個(gè)模板dom元素 var $tmplTr = $("#tmpl01").tmpl(peopleArr);//返回的是jq對(duì)象.這里是老師故意分開寫 ohyeah $(function () { $tbCreate = $("<table></table>").append($tmplTr); //將table加到body里面去 $(document.body).append($tbCreate); }) </script>

 三.Jquery中的操作class的幾個(gè)方法

<html xmlns="http://www.w3.org/1999/xhtml"><head>    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />    <title></title>    <script src="jquery/jquery-1.9.1.js"></script>    <style type="text/CSS">        div {            border: 1px solid #0094ff;            height: 100px;            width: 100px;        }        .red {            background-color: red;        }        .black {            background-color: black;        }    </style>    <script type="text/Javascript">        $(function () {            $("#btnChange").click(function () {                //切換class//toggleClass 會(huì)判斷 是否有這個(gè)class  如果有 刪除,如果沒有,就添加
API中的說明:如果存在(不存在)就刪除(添加)一個(gè)類 $("div").toggleClass("black"); }) $("#btnRedAdd").click(function () { //增加class $("div").addClass("red"); }) $("#btnRedRemove").click(function () { //刪除class $("div").removeClass("red"); }) }) </script></head><body> <input type="button" value="黑白切換" id="btnChange" /> <input type="button" value="增加redclass" id="btnRedAdd" /> <input type="button" value="移除redclass" id="btnRedRemove" /> <div></div> <div></div> <div></div></body></html>

 四:jq里面的克隆

jq里面的克隆,不管深淺,都會(huì)克隆子節(jié)點(diǎn),jq里面的淺克隆,只是克隆元素,事件不克隆,jq里面的深克隆,事件一并克隆了

<html xmlns="http://www.w3.org/1999/xhtml"><head>    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />    <title></title>    <script src="jquery/jquery-1.9.1.js"></script>    <script type="text/javascript">        var $button = $("<input type='button' value='點(diǎn)擊刪除自己' onclick='$(this).remove();'/>");        //為button設(shè)置點(diǎn)擊事件        //使用jq的方法為dom元素增加事件,當(dāng)這個(gè)dom元素從dom樹里面移除,事件也沒有了        //如果要實(shí)現(xiàn),將dom元素從dom樹里面移除,事件還在,可以將事件寫道dom元素的 事件屬性里面        $(function () {            $("#btnAdd").click(function () {                //將 按鈕 追加到body里面去                $(document.body).append($button);            });        })    </script></head><body>    <input type="button" id="btnAdd" value="增加按鈕" /></body></html>

 五:屬性過濾器

<html xmlns="http://www.w3.org/1999/xhtml"><head>    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />    <title></title>    <script src="jquery/jquery-1.9.1.js"></script>    <script type="text/javascript">        alert("稍等一會(huì)");        $(function () {            //表示擁有某種屬性的元素            $("li[skill]").css("backgroundColor", "orange");            //表示屬性等于某個(gè)值            $("li[skill=noNice]").css("fontSize", "50px");            //表示屬性不等于某個(gè)值            $("li[skill!=noNice]").css("color", "#0094ff");            //屬性過濾器,可以判斷任意屬性-包括id,class等            //適用范圍,就是對(duì)于元素的一個(gè)過濾            $("li[class]").css("border", "5px solid #0094ff");            $("li[class=vegetable][PRize=10]").css("backgroundColor", "green");        })    </script></head><body>    <ol>        <li skill="noSwim">惡魔果實(shí)</li>        <li class="noGoodLook">百香果</li>        <li skill="noNice">榴蓮</li>        <li class="vegetable" prize="5">西蘭花</li>        <li class="vegetable" prize="10">秋葵</li>        <li id="smile">開心果</li>        <li class="noDelicious">韭菜月餅</li>    </ol></body></html>

六:表單元素過濾器

<html xmlns="http://www.w3.org/1999/xhtml"><head>    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />    <title></title>    <script src="jquery/jquery-1.9.1.js"></script>    <script type="text/javascript">        $(function () {            $("#btnShowNum").click(function () {                //使用表單元素過濾器 獲取選中的checkbox                alert($("input[type=checkbox]:checked").length);            })        })    </script></head><body>    <input type="checkbox" /><label>籃球</label>    <input type="checkbox" /><label>足球</label>    <input type="checkbox" /><label>排球</label>    <input type="checkbox" /><label>曲棍球</label>    <br />    <input type="radio" name="male" /><label>男</label>    <input type="radio" name="male" /><label>女</label>    <input type="button" value="顯示選中個(gè)數(shù)" id="btnShowNum" /></body></html>

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 谢通门县| 广汉市| 利津县| 高陵县| 甘孜县| 九龙城区| 巨鹿县| 诸暨市| 安宁市| 六安市| 兴山县| 岳阳县| 准格尔旗| 济南市| 陆良县| 文昌市| 武宣县| 武安市| 安平县| 平和县| 平定县| 阿合奇县| 渝中区| 祥云县| 沈阳市| 恩施市| 清苑县| 嵊泗县| 来凤县| 涪陵区| 会泽县| 蒙自县| 城固县| 金秀| 丰台区| 甘泉县| 宣城市| 拉萨市| 武强县| 高阳县| 五原县|