[英]Best way to implement .lastChild using Prototype or jQuery
Currently we are using prototype and jQuery as our js frameworks. 目前我们正在使用prototype和jQuery作为我们的js框架。 Right now, jQuery is set to $j() to prevent conflicts from prototype. 现在,jQuery设置为$ j()以防止原型冲突。
In the past, we've used a lot of prototype's Element.down(), Element.next(), and Element.previous() to traverse the DOM. 在过去,我们使用了很多原型的Element.down(),Element.next()和Element.previous()来遍历DOM。 However, I need a simple way to retrieve the last child element. 但是,我需要一种简单的方法来检索最后一个子元素。 I know i can loop through an array by using Element.childElements() but I would like something inline that reads cleanly and can be pipelined. 我知道我可以通过使用Element.childElements()来遍历一个数组,但我想要一些内联,它可以干净地读取并且可以流水线化。
Just thought I would ask before I go reinventing the wheel. 我想在重新发明轮子之前我会问。 Here's a snippet of code that has lastChild in it that needs to be replaced: 这里有一段代码,其中包含需要替换的lastChild:
_find : function(rows, address) {
var obj = null;
for (var i=0; i < rows.length && obj == null; i++) {
if (rows[i].down().className == 'b')
obj = this._find(rows[i].lastChild.down().down().childElements(), address);
else if (rows[i].lastChild.getAttribute('tabAddress') == address)
return rows[i].lastChild;
}
return obj;
}
Guys, note that the selector functions return arrays of elements (not single elements), so you must adddress the element in the result array by index: [0]. 伙计们,请注意,选择器函数返回元素数组(而不是单个元素),因此必须通过索引在结果数组中添加元素:[0]。
Code in prototype 原型代码
//if you only have the id of the parent
var lastChild = $$("#parent :last-child")[0];
//or
//if you have the actual DOM element
var lastChild = $(element).select(":last-child")[0];
Code in Jquery Jquery中的代码
//if you only have the id of the parent
var lastChild = $("#parent :last-child")[0];
//or
//if you have the actual DOM element
var lastChild = $(":last-child", element)[0];
Code in plain vanilla javascript 简单的香草javascript中的代码
var element = document.getElementById("parent");
var lastChild = element.childNodes[element.childNodes.length - 1];
Also note that these can return null if the parent element has no child nodes. 另请注意,如果父元素没有子节点,则这些元素可以返回null。
Try this it has always worked for me in jQuery 试试这个它一直在jQuery中为我工作
var lastChild = $("#parent :last-child");
http://docs.jquery.com/Selectors/lastChild http://docs.jquery.com/Selectors/lastChild
使用Prototype,您可以使用支持大多数CSS3语法的$$实用程序函数:
var lastChild = $$(".b:last-child")[0];
如果有人在搜索网页时发现这个问题,可以使用Prototype:
Element.childElements().last();
For anyone else's reference who still uses Prototype, You can add custom element methods: 对于仍然使用Prototype的其他任何人的参考,您可以添加自定义元素方法:
Element.addMethods({ ... });
I added these two among others: 我添加了这两个:
first: function(element) {
element = $(element);
return element.childElements()[0];
},
last: function(element) {
element = $(element);
var i = element.childElements().size() - 1;
return element.childElements()[i];
}
var first = $('foo').first();
var last = $('foo').last();
The Element.first()
method is sort of redundant, I use it for cleaner-looking code when chaining. Element.first()
方法有点多余,我在链接时使用它来获得更清晰的代码。
Make sure you reutrn element
to enable chaining. 确保你reutrn element
以启用链接。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.