jquery完成挑选作用的方式:1、应用jquery的appendTo让点一下的原素添加到列举项里边;2、根据jquery的clone函数完成立即清除特性目录里边的视频就可以。
文中实际操作自然环境:Windows7系统软件、jquery3.2.1、Dell G3电脑上。
jquery如何完成挑选作用?
jquery 完成挑选作用。
商品属性挑选
产品的特性挑选 是一个十分普遍的作用,通常是同一种类的特性只有挑选一个。比如 价钱范畴,或是知名品牌,为了更好地使选定的具体内容看起来更形象化 ,一般都是会将选择项列举,并有着点一下撤消 挑选 ,点一下类似更换的作用。
在下面得出2个详细完成。
完成1:
应用jquery 的appendTo 让点一下的原素添加到列举项里边 (与此同时检验列举项是不是为同一类的原素,有则更换出去,使它回到原目录),
在列举项里边点一下原素 ,则回到 原div ,这儿为了更好地完成 回到原先的div 应用了自定特性 。
当然这一完成是有一定的问题,因为减少了div 的class 和父原素的id 一致,这类状况很不方便 ,
并且每一次都是会必须再次关联全部事情,一个递归。高效率上有些缺乏,
下面看第二种构思 ,更简易清楚一点。高效率会高些一点,并且不用id 和class 同样 ,也不用再次关联好几个事情
<!DOCTYPE html> <html> <head> <title>动漫</title> <style type="text/css"> .class1,.class2{ width: 100px; height: 40px; margin: 10px; } #count{ background-color: sandybrown; width: 400px; height: 200px; } </style> </head> <body> <div id='class1'> <button class="class1" data-belong="class1">真皮皮鞋</button> <button class="class1" data-belong="class1">凉拖</button> <button class="class1" data-belong="class1">凉拖</button> </div> <div id="class2"> <button class="class2" data-belong="class2">胶手套</button> <button class="class2" data-belong="class2">胶皮手套</button> <button class="class2" data-belong="class2">毛胶手套</button> </div> <div id="count"></div> </body> </html> <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script> <script type="text/javascript"> (function check(){ $('#class1> button,#class2>button').off("click").on("click",function(){ $("#count button." $(this).attr("data-belong")).appendTo("#" $(this).attr("data-belong")); $(this).appendTo("#count"); check() }) $('#count button').off("click").on("click",function(){ $(this).appendTo("#" $(this).attr("data-belong")) check() }) })() </script>
完成2:
应用jquery的clone 涵数 ,那样原目录就不容易必须修改 ,点一下 特性目录里边的信息只要立即清除,不用id 和 class一致。
更独立一些 。编码降低了,沒有递归的,沒有数据信息改动关联的问题,比完成1 提升许多
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> </head> <style type="text/css"> .class1,.class2{ width: 100px; height: 40px; margin: 10px; } #count{ background-color: sandybrown; width: 400px; height: 200px; } </style> <body> <div> <button class="class1" data-belong="class1">真皮皮鞋</button> <button class="class1" data-belong="class1">凉拖</button> <button class="class1" data-belong="class1">凉拖</button> </div> <div> <button class="class2" data-belong="class2">胶手套</button> <button class="class2" data-belong="class2">胶皮手套</button> <button class="class2" data-belong="class2">毛胶手套</button> </div> <div class="count"> </div> </body> </html> <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script> <script type="text/javascript"> //只需维持 button 的 class 标示 和data-belong 一致就可以。 $("div:lt(2) button").click(function(){ $("div.count ." $(this).attr('data-belong')).remove(); $(this).clone().appendTo("div.count"); $('.count button').off("click").on("click",function(){ $(this).remove(); }) }) </script>
建议学习培训:《jquery视频教程》
以上便是jquery如何完成挑选作用的详尽具体内容,大量请关心自学java网其他相关文章!
WWW.lllT.neT声明:有的资源来自网络转载,版权归原作者所有,如有侵犯到您的权益请联系邮箱:our333@126.com我们将配合处理!
原文地址:jquery如何完成挑选作用发布于2021-12-11 16:09:01