Mootools从父元素获取子元素索引
Mootools get the child-index of an element from its parent
我在mootools中使用事件委托。我想知道被点击的行号。我的解决方案如下图所示:是否有比我目前所做的更好的方法?
我的方法是比较元素,直到找到匹配。我可以使用IndexOf方法吗?
(以下是来自jsfiddle的用于将来的数据)
HTML:<div id="Record_List">
<div class="Row">
<input type="submit" name="Row" value="Edit"/>
</div>
<div class="Row">
<input type="submit" name="Row" value="Edit"/>
</div>
</div>
Javascript: window.addEvent(
'domready',
function()
{
$('Record_List').addEvent(
'click:relay(input)',
function(evt, target)
{
evt.stop();
var rowElem = target.getParent();
var rowNumber = -1;
$('Record_List').getChildren('div.Row').each(
function (el, num)
{
if (rowElem === el)
{
rowNumber = num;
}
});
// Find the position of the row and display it here:
alert('Row number: ' + rowNumber);
});
});
getChildren
返回的类型(Elements
)包含Array
个方法,包括indexOf
。如果浏览器不存在该方法,MooTools将提供该方法的实现。考虑到这一点,您可以这样写:
$('Record_List').getChildren('div.Row').indexOf(rowElem);
更新示例: http://jsfiddle.net/andrewwhitaker/uJarB/
这是相当粗糙的,但你总是可以使用Array.prototype
的indexOf
…
Array.prototype.indexOf.call(list-o-children, elem-to-find)
MooTools Element.getAllPrevious
返回当前元素之前的所有兄弟元素,前一个兄弟元素的长度等于当前元素的索引。
var index = rowElem.getAllPrevious('div.Row').length;
相关文章:
- 如何索引jquery中的每个元素
- JavaScript DOM:在容器中查找元素索引
- 通过javascript中的元素索引访问eventListener中的元素
- 将页面元素索引到 JSON 对象?或者jQuery选择器它每次
- jQuery将相同的类添加到两个不同容器中的同一元素索引中
- 获取小元素索引直到数组长度
- jQuery获取元素索引,尽管包装器/父元素
- 更改数组中某些元素(索引)的顺序
- 访问具有公共类名的元素数组中的元素索引
- 访问循环的内部的ES6数组元素索引
- 列表类元素(索引).渐变(某些速度)不起作用
- javascript中的表单元素索引,用于jquery.change函数
- 获取文本块中的元素索引
- 用主干JS .get模型事件连接元素索引
- 从81个元素索引的数组中获取行和列值
- 如何使用javascript获得元素索引
- 使用类和子类获取元素索引
- Mootools从父元素获取子元素索引
- 如何在Javascript中获得一组固定元素的元素索引
- 使用jQuery更改列表元素索引