简体   繁体   English

使用原型获取表中的最后一个tr

[英]Fetch the last tr in a table using prototype

The question is related to read td values using prototype 问题与使用原型读取td值有关

On a similar case, how do I fetch the last row in the following table, 在类似情况下,如何获取下表中的最后一行,

<table id="myTable">
 <tr>
   <td>apple</td>
   <td>orange</td>
  </tr>
  <tr>
    <td>car</td>
    <td>bus</td>
  </tr>
</table>

And how do you guys learn these methods and javascript libraries, I find it strenuous. 你们如何学习这些方法和javascript库,我觉得这很费力。 The examples given in their website are not that good for a beginner :( 他们的网站上提供的示例对初学者来说不是那么好:(

Thanks, J 谢谢,J

As Dr.Molle just said, $$ returns an array. 正如Dr.Molle所说,$$返回一个数组。 Knowing this you can come up with a shorter answer. 知道这一点,您可以得出一个简短的答案。

$$('#myTable tr').last()

And in my eyes is easier to read too without the [0] . 而且在我眼中,没有[0]也更容易阅读。 Because a table may have rows in a <tfoot> as well I would stick a tbody in that selector. 因为一个表在<tfoot>中也可能有行,所以我会将tbody粘贴在该选择器中。

Personally I find Prototype easier to remember in general because it is easier to read then the supposedly simpler jQuery. 就个人而言,我发现Prototype通常更容易记住,因为它比所谓的更简单的jQuery更容易阅读。 In some way it better suits my programming style. 在某种程度上,它更适合我的编程风格。 That might be the secret, match a language/library/framework to your personality and not the other way around or you'll be needing therapy. 这可能是秘密,将语言/图书馆/框架与您的个性相匹配,而不是相反,否则您将需要治疗。 You could validly ask the same on Programmers SE . 您可以在Programmers SE上有效地询问相同的问题。

You can fetch the tr using: 您可以使用以下方法获取tr:

$$('#myTable tr:last-child')[0]

$$ returns, different from $, an array(with the example above it contains 1 element), with [0] you select the first element in this array. $$返回不同于$的数组(上面的示例包含1个元素),用[0]选择此数组中的第一个元素。

Regarding to your additional question: prototypejs's documentation in my eyes is really bad, but there are frameworks with good api-documentations. 关于您的其他问题:在我看来,prototypejs的文档确实很糟糕,但是有些框架具有良好的api文档。

Maybe you better try another library(jQuery for example should be easy do understand, even to beginners, and is well documented too). 也许您最好尝试使用另一个库(例如,即使是初学者,jQuery也应该很容易理解,并且也有据可查)。 I don't think that prototypejs, like it is now, will have a bright future. 我认为,像现在这样的prototypejs不会有光明的未来。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM