如何使用javascript获得div的价值 [英] How to get value of a div using javascript

查看:79
本文介绍了如何使用javascript获得div的价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的div:

 < div id =demoalign =centervalue =1 > 
< h3>按颜色< / h3>
< input type =radioname =group1id =color1value =#990000onClick =changeColor()/>< label for =color1> Red< /标签>
< input type =radioname =group1id =color2value =#009900onClick =changeColor()/>< label for =color2> Green< /标签>
< input type =radioname =group1id =color3value =#FFFF00onClick =changeColor()/>< label for =color3> Yellow< /标签><峰; br><峰; br><峰; br>
< / div>

我想要div的值属性(value =1)。



我想这样:

  function overlay()
{
var cookieValue = document.getElementById(demo).value;
alert(cookieValue);
}

但它显示未定义如何使用javascript获取值1任何解决方案。

解决方案

DIV s没有 value property

从技术上讲,根据DTD,它们不应该有 value 属性 ,但通常您会希望在此使用 .getAttribute() case:

 函数覆盖()
{
var cookieValue = document.getElementById('demo') .getAttribute( '值');
alert(cookieValue);
}


This is my div:

<div id="demo" align="center"  value="1">
    <h3>By Color</h3>
    <input type="radio" name="group1" id="color1" value="#990000"  onClick="changeColor()"/><label for="color1">Red</label>
    <input type="radio" name="group1" id="color2" value="#009900" onClick="changeColor()"/><label for="color2">Green</label>
    <input type="radio" name="group1" id="color3" value="#FFFF00" onClick="changeColor()" /><label for="color3">Yellow</label><br><br><br>
</div>

I want the value attribute of that div (value="1").

I am trying like this:

function overlay()
{
    var cookieValue = document.getElementById("demo").value;    
    alert(cookieValue);
}

but it is showing "Undefined" how to get value 1 using javascript please suggest any solution,.

解决方案

DIVs do not have a value property.

Technically, according to the DTDs, they shouldn't have a value attribute either, but generally you'll want to use .getAttribute() in this case:

function overlay()
{
    var cookieValue = document.getElementById('demo').getAttribute('value');
    alert(cookieValue);
}

这篇关于如何使用javascript获得div的价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆