使用原型在标签中获取for的值

时间:2012-05-15 19:28:14

标签: javascript prototypejs

HTML:

<label name="table_id" for="1280461">Rock</label>
<label name="table_id" for="1291065">Paper</label>

这不起作用:

$$('label[name="table_id"]').each(function(s){ 
  console.log(s.for); 
});

3 个答案:

答案 0 :(得分:1)

s.readAttribute('for');

请参阅:http://prototypejs.org/api/element/readattribute

答案 1 :(得分:1)

for是Javascript中的关键字。您无法以“s.for”形式使用它。

使用DOM Element的{​​{3}}功能:

$$('label[name="table_id"]').each(function(s){ 
    console.log(s.getAttribute('for')); 
});

答案 2 :(得分:0)

我创造了一个小提琴来回答这个问题。希望这对你有意义。

http://jsfiddle.net/QmEWD/