Mootools 从父元素获取元素的子索引
我在 mootools 中使用事件委托。我想知道被点击的行号。我的解决方案如下所示 jsfiddle:有比我目前正在做的更好的方法吗?
我的方法是比较元素,直到找到匹配项。我可以使用 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);
});
});
I am using event delegation in mootools. I want to know the row number that has been clicked. My solution is shown in this jsfiddle: Is there a better way than what I am currently doing?
My approach was to compare the elements until i found the match. Is there an IndexOf method I could use?
(The following is the data from the jsfiddle for the future)
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);
});
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
getChildren
返回的类型 (Elements
) 包含Array
方法,包括indexOf
。如果浏览器不存在该方法,MooTools 将提供该方法的实现。考虑到这一点,您可以编写:更新的示例: http://jsfiddle.net /andrewwhitaker/uJarB/
The type (
Elements
) returned bygetChildren
containsArray
methods, includingindexOf
. MooTools will provide an implementation of that method if it does not exist for the browser. With that in mind, you could write:Updated example: http://jsfiddle.net/andrewwhitaker/uJarB/
这非常hacky,但您始终可以使用
Array.prototype
的indexOf
...This is pretty hacky, but you can always use
Array.prototype
'sindexOf
...MooTools
Element.getAllPrevious
返回当前元素之前的所有兄弟元素,前一个兄弟元素的长度等于当前元素的索引。MooTools
Element.getAllPrevious
returns all sibling elements before the current element, the length of previous siblings equals to the index of the current element.