prev() 函数被用来匹配元素集的前一个兄弟元素,仅仅只有前一个兄弟元素被选择,其子元素将被忽略。本文主要介绍了jquery里prev()的简单操作代码,非常不错,具有参考借鉴价值,需要的朋友可以参考下,希望能帮助到大家。
一个朋友提了个需求给我:点击按钮删除input,如果input剩一个,点击按钮全部消失。
很简单的操作,但是你如果不知道jquery里有prev()这个方法,可能你会走很多弯路。
代码:
html
<p> <input type="text" placeholder="用户名"> <input type="text" placeholder="用户名"> <input type="text" type="hidden"> <input type="text" type="hidden"> <a class="reduce" onclick="less()">—</a> </p>
css
.reduce{
display: inline-block;
color: white;
text-align: center;
width: 30px;
height: 30px;
background: red;
line-height: 30px;
cursor: pointer;
}
input{
height: 18px;
padding: 5px 10px;
}JS
<script src="jquery-1.7.2.min.js"></script>
<script>
var Reduce = document.getElementsByClassName("reduce");
var Inp = document.getElementsByTagName("input");
function less(){
//查找元素的上一个元素 并移除
$(".reduce").prev().remove();
if(Inp.length < 1){
$(".reduce").remove()
}
}
$(".reduce")
</script>这里我混合写的,不规范,只做一个栗子,重在理解。
相关推荐:
以上就是实例分析关于jQuery里prev()的简单操作代码的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号