你如何使用Jquery在链中倒退?

cho*_*bo2 17 jquery

我认为jquery中有一些东西允许你通过链条向后走.

我想做这个:

var a = $(this).parent().addClass('test').goBackToParent().children('selectorHere').text()
Run Code Online (Sandbox Code Playgroud)

我想获取我拥有的对象的父级并为其添加类名.一旦我这样做,我想通过它的孩子,找到一个匹配我的选择器并获得其文本的孩子.

我可以这样做,还是我必须这样做:

$(this).parent().addClass('test');
var a = $(this).parent().children('selectorHere').text()
Run Code Online (Sandbox Code Playgroud)

编辑

我现在正在使用"结束",但它不起作用我做了一个例子,你可以在这里尝试

<!DOCTYPE html>
<html>
<head>
<script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<meta charset=utf-8 />
<title>JS Bin</title>
<!--[if IE]>
  <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<style>
  article, aside, figure, footer, header, hgroup,
  menu, nav, section { display: block; }
</style>
</head>
<body>
  <table>
    <th>
      <tr>one</tr>
      <tr>two</tr>
    </th>
    <tr>
      <td id="test"><a href="#"><img src="http://ais.web.cern.ch/ais/apps/hrt/SMT%20v2/Images/btnbar_edit.png" /></a></td>
      <td class="selectMe">1</td>
    </tr>
  </table>
</body>
</html>


$(function()
  {
      $('#test').click(function()
                       {
                           // does not work
                           var a = $(this).parent().addClass('afb').end().children('.selectMe').text();
                           // works
                           var b = $(this).parent().children('.selectMe').text();
                         alert(a + ' -should have value');
                          alert(b + ' -works');
                         return false;
                       });
  });
Run Code Online (Sandbox Code Playgroud)

Dex*_*ter 29

你正在寻找end()方法:

var a = $(this).parent().addClass('test').end()
               .children('selectorHere').text();
Run Code Online (Sandbox Code Playgroud)

方法:

结束当前链中的最新过滤操作,并将匹配元素集返回到其先前状态.

更新:基于您的html片段,您实际上根本不需要end!关于jQuery的重要一点是,大多数非遍历方法返回自己的副本 - 所以如果你调用$(this).addClass('test'),你会得到$(this)的副本.

所以对于你的代码片段:

// does not work
var a = $(this)                       // points at #test
           .parent()                  // now refers to the tr which holds test
               .addClass('afb')       // still refers to the tr
               .end()                 // now we're back to #test
           .children('.selectMe')     // empty! there are no direct children of #test which match .selectMe
               .text();               // empty
Run Code Online (Sandbox Code Playgroud)

相反,尝试没有结束:

// works
var a = $(this)                       // points at #test
           .parent()                  // now refers to the tr which holds test
               .addClass('afb')       // still refers to the tr
           .children('.selectMe')     // found the .selectMe td
               .text();               // returns '1'
Run Code Online (Sandbox Code Playgroud)


Ale*_*rge 7

您正在寻找该方法end().只需用它代替goBackToParent().

大多数jQuery的DOM遍历方法都在jQuery对象实例上运行,并生成一个新的,与不同的DOM元素集匹配.当发生这种情况时,就好像将新的元素集推送到在对象内维护的堆栈上.每个连续的过滤方法将新元素集推送到堆栈上.如果我们需要一个较旧的元素集,我们可以使用end()从堆栈中弹出集合.