简体   繁体   English

使用Mootools css选择器获取具有相同ID的相同元素的元素

[英]Get Element with Mootools css selector for same element with same id

I have a table which has several links in td tag with the same id,can I get the link's inner html respectively.I want to know 我有一个表在td标签中有几个具有相同id的链接,我可以分别得到链接的内部html。我想知道

  1. Why cant I add events with all the same elements with same id as $$('#update_room_link') but class selector as $$('.update_room_link') works 为什么我不能添加具有与$$相同的所有相同元素的事件('#update_room_link'),但类选择器为$$('。update_room_link')工作
  2. 2.How to get the innerhtml for respective links. 2.如何获取各个链接的innerhtml Same id but different innerhtml upon clicking links, heres the jsfiddle 在点击链接时,相同的id但不同的innerhtml ,继承人jsfiddle

the is what I want - 这就是我想要的 -

<table>
    <tr>
        <td> <a href="javascript:void(0)" class="update_room_link">Edit</a>

        </td>
        <td> <a href="javascript:void(0)" class="update_room_link">Delete</a>

        </td>
        <td> <a href="javascript:void(0)" class="update_room_link">Add</a>

        </td>
    </tr>
</table>

javascript tried as javascript试过

$$('#update_room_link').addEvent('click', function () {
    alert();// get innerHtml of Edit,Delete or Add as clicked

})

Questions updated 问题已更新
I have a html SELECT in a table row,this row has many table data.I want to get the value/innerhtml of a table data which has its id set (and within the SAME row).Or how can I get an immediate grand-parent of a child FROM a child..is it possible 我在表行中有一个html SELECT,这行有很多表数据。我想得到一个表数据的值/ innerhtml,它的id设置(并在SAME行中)。或者我怎么能得到一个直接的盛大 - 孩子的父母 - 孩子......这是可能的

<tr> 
<td id="id">100</td> 
<td>Australi</td> 
<td>BAT</td> 
<td>2014-02-23</td> 
<td>pending</td> 
</tr>

在此输入图像描述

I want the innerhtml of name of the SELECTED option. 我想要SELECTED选项的名称的innerhtml。 All the select has same class name. 所有选择都具有相同的类名。

The ID must be unique in a html element, that's why you can not use the same ids on your a tags so $$('#update_room_link') is for all elements. ID必须在html元素中是唯一的,这就是为什么你不能在你a标签上使用相同的id,所以$$('#update_room_link')适用于所有元素。

You can use the class and can get inner html like this 你可以使用这个类,并可以获得这样的内部HTML

$$('.update_room_link').addEvent('click', function () {
    alert(this.innerHTML); //here you will get the respective inner HTML
})

DEMO DEMO

Please see ID shold be unique 请参阅ID shold是独一无二的

If you want use id then it should be unique, in that case you will have to attach each function independntly, which is not good practice, like 如果你想使用id那么它应该是唯一的,在这种情况下你必须独立地附加每个函数,这不是好习惯,比如

$$('#update_room_link1').addEvent('click', function () {....
$$('#update_room_link2').addEvent('click', function () {....

I add this answer after you already accepted a answer, just to complete a bit. 我已经接受了答案之后添加了这个答案,只是为了完成一点。

If you want to add event to one element by ID you can use 如果您想通过ID向一个元素添加事件,则可以使用

$('update_room_link').addEvent('click', function () {

it will also work with 它也可以使用

$$('#update_room_link').addEvent('click', function () {

To add events to many elements, by CLASS you can use 要向多个元素添加事件,可以使用CLASS

$$('.update_room_link').addEvent('click', function () {

To get the HTML the recommended method by MooTools is 获得HTML ,MooTools推荐的方法是

element.get('html'); 

but will also work with 但也会合作

element.innerHTML

since mootools uses native elements 因为mootools使用原生元素

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

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