使用 javascript、mootools 获取 HTML 元素对象
请检查下面的 HTML:
<table cellpadding="0" cellpadding="0" border="0">
<tr>
<td>
<div class="toogler">Demo1</div>
</td>
</tr>
<tr>
<td>
<div class="element">Demo1 Content</div>
</td>
</tr>
<tr>
<td>
<div class="toogler">Demo1</div>
</td>
</tr>
<tr>
<td>
<div class="element">Demo1 Content</div>
</td>
</tr>
<tr>
<td>
<div class="toogler">Demo2</div>
</td>
</tr>
<tr>
<td>
<div class="element">Demo2 Content</div>
</td>
</tr>
<tr>
<td>
<div class="toogler">Demo3</div>
</td>
</tr>
<tr>
<td>
<div class="element">Demo3 Content</div>
</td>
</tr>
<tr>
<td>
<div class="toogler">Demo4</div>
</td>
</tr>
<tr>
<td>
<div class="element">Demo4 Content</div>
</td>
</tr>
</table>
这是我的 JS 代码:
<script type="text/javascript" language="javascript">
$$('.toogler').each(function(e){
alert(e);
// this will alert all the toogler div object
});
</script>
我的问题是,
如果我有第一个 toolgler 的对象,我如何使用类元素获取下一个 div 的对象,那么我如何获取下一个第一个 div 的对象class 'element'
我不想给元素提供 id
Please check my HTML below:
<table cellpadding="0" cellpadding="0" border="0">
<tr>
<td>
<div class="toogler">Demo1</div>
</td>
</tr>
<tr>
<td>
<div class="element">Demo1 Content</div>
</td>
</tr>
<tr>
<td>
<div class="toogler">Demo1</div>
</td>
</tr>
<tr>
<td>
<div class="element">Demo1 Content</div>
</td>
</tr>
<tr>
<td>
<div class="toogler">Demo2</div>
</td>
</tr>
<tr>
<td>
<div class="element">Demo2 Content</div>
</td>
</tr>
<tr>
<td>
<div class="toogler">Demo3</div>
</td>
</tr>
<tr>
<td>
<div class="element">Demo3 Content</div>
</td>
</tr>
<tr>
<td>
<div class="toogler">Demo4</div>
</td>
</tr>
<tr>
<td>
<div class="element">Demo4 Content</div>
</td>
</tr>
</table>
Here is my JS Code:
<script type="text/javascript" language="javascript">
$('.toogler').each(function(e){
alert(e);
// this will alert all the toogler div object
});
</script>
my problem is that how can i fetch the object of the next div with class element
if i have object of the first toogler then how can i get the object of the next first div which class 'element'
I don't want to give the ids to the elements
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您无法按照 oskar(最好情况)的建议更改 html 输出和重构,则此方法有效:
e.getParent().getParent().getNext().getFirst().getFirst()
- 它会返回下一个 div,但速度很慢。不幸的是,表破坏了 .getNext("div.element") 因为它不是同级。
另一种可行的方法是(如果它们的长度匹配) - 如果将引用作为 1-off 放入元素存储中,速度会快得多:
但我不喜欢这两种解决方案,因为它们的可维护性/可扩展性不够。
if you can't alter the html output and refactor as suggested by oskar (best case), this works:
e.getParent().getParent().getNext().getFirst().getFirst()
- it will return you the next div but it's slow.unfortunately, tables break .getNext("div.element") as it's not a sibling.
another way that works is this (if their lengths match) - it will be MUCH faster if the reference is put in element storage as a 1-off:
i don't like either solution though, not maintainable / scalable enough.
您必须逐一迭代并检查该类。
You shall have to iterate through and check for the class one by one.
将切换器分配给切换元素的最简单方法:
这是一个工作示例: http://jsfiddle.net/oskar /aTaBB
另外,去掉桌子。
The easiest way of assigning a toggler to the toggled element:
Here's a working example: http://jsfiddle.net/oskar/aTaBB
Also, get rid of the table.
尝试使用 Element.getNext([match])。
Try using Element.getNext([match]).