简体   繁体   English

如何获得h2元素的值

[英]how to get value of h2 element

How can I get the content of h2 tag in Prototype framework? 如何在Prototype框架中获取h2标签的内容?

I tried this: 我尝试了这个:

alert($$('h2').value());

but it haven't done anything. 但它什么也没做。

thx, 谢谢,

Oded 奥德

edit: thank you for the fast and great support! 编辑:感谢您的快速和大力的支持!

The $$ returns an Array. $$返回一个数组。

I you only want the first <h2> , then access it at index 0 . 我只需要第一个<h2> ,然后从索引0访问它。

alert($$('h2')[0].innerHTML);

Or you can iterate over the Array using prototypejs' .each() method. 或者,您可以使用prototypejs的.each()方法遍历数组。

$$('h2').each(function(el,i) {
    alert(el.innerHTML);
});

$$(el) creates an array. $$(el)创建一个数组。 You need to iterate over each value, or if you just want the first h2, then use .first() 您需要遍历每个值,或者如果您只想要第一个h2,则使用.first()

alert($$('h2').first().value());

Check out the API http://globalmoxie.com/bm~doc/prototype-160-api.pdf 查看API http://globalmoxie.com/bm~doc/prototype-160-api.pdf

Shouldnt you use .html(). 您不应该使用.html()。 jQuery is using this, i think prototype would use something similar. jQuery正在使用此工具,我认为原型将使用类似的工具。

Try this: alert($('h2').innerHTML); 试试这个:alert($('h2')。innerHTML);

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

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