其他分享
首页 > 其他分享> > js技术之获取当前元素的上一个/下一个兄弟级元素等元素的方法(获取上一个/下一个input)

js技术之获取当前元素的上一个/下一个兄弟级元素等元素的方法(获取上一个/下一个input)

作者:互联网

一.说明

jQuery获取:

jQuery.parent(expr),找父亲节点,可以传入expr进行过滤,比如$("span").parent()或者$("span").parent(".class")

jQuery.parents(expr),类似于jQuery.parents(expr),但是是查找所有祖先元素,不限于父元素

jQuery.children(expr),返回所有子节点,这个方法只会返回直接的孩子节点,不会返回所有的子孙节点

jQuery.contents(),返回下面的所有内容,包括节点和文本。这个方法和children()的区别就在于,包括空白文本,也会被作为一个jQuery对象返回,children()则只会返回节点

jQuery.prev(),返回上一个兄弟节点,不是所有的兄弟节点

jQuery.prevAll(),返回所有之前的兄弟节点

jQuery.next(),返回下一个兄弟节点,不是所有的兄弟节点

jQuery.nextAll(),返回所有之后的兄弟节点

jQuery.siblings(),返回兄弟姐妹节点,不分前后

jQuery.find(expr),跟jQuery.filter(expr)完全不一样:

jQuery.filter(),是从初始的jQuery对象集合中筛选出一部分,而

jQuery.find(),的返回结果,不会有初始集合中的内容,比如$("p").find("span"),是从<p>元素开始找<span>,等同于$("p span")

二.案例

JSP控件

<td>
   <input type="text" name="products.purchasePrice" value="${product.purchasePrice}">
</td>
<td>
   <input type="text" thanSalesPrice="${loopStatus.index }" name="products.salesPrice" value="${product.salesPrice}">
</td>

 

js校验

// 判断
jQuery.validator.addMethod("thanSalesPrice", function(value, element, param) {
// 输入salesPrice参数时获取purchasePrice元素对象(获取上一个input对象)
var purchasePriceObj = $(element).parent().prev().find("input");

// purchasePrice的value值
var purchasePrice = purchasePriceObj.val();

  if (purchasePrice < value)
  {
    // 提示
    return true;
  }

  // 不会提示
  return false;
}, "提示内容:purchasePrice < salesPrice");

 

标签:jQuery,返回,purchasePrice,expr,元素,js,获取,节点
来源: https://www.cnblogs.com/saoge/p/16257635.html