2

I want to get value of value attribute using id attribute of html tag.

<p id='abc' value='xyz'>123456</p>

and my javascript is

var a=document.getElementById(abc).innerHTML; 
alert(a);

this gives me alert "123456" but I want alert function should display me "xyz". How can I do these. I also tried getElementById(abc).value this returns nothing.

OR is there ant way to make tag which is doesn't display on browser. I don't want to display 123456 on browser.

4

3 回答 3

5

尝试这个

var a = document.getElementById('abc').getAttribute('value'); 
alert(a);

请注意,abc您问题中的论点未用引号引起来。

于 2013-02-19T05:57:20.910 回答
2

尝试这个

alert(document.getElementById("abc").getAttribute("value"));

DOM Element getAttribute() 方法 - 获取链接的目标属性值:

演示

于 2013-02-19T05:58:34.440 回答
1

尝试以下操作:

var a=document.getElementById("abc").getAttribute("value");
于 2013-02-19T05:59:38.690 回答