Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/422.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将html元素值添加到javascript对象属性_Javascript_Html_Object - Fatal编程技术网

将html元素值添加到javascript对象属性

将html元素值添加到javascript对象属性,javascript,html,object,Javascript,Html,Object,我有一个javascript对象和一个html表 var myObj=[ { 价格:1,, 标签:“一” } ]; 100 产品 你是说 myObj.price=parseInt(document.getElementById("price").innerHTML,10); 对于整数或 myObj.price=parseFloat(document.getElementById("price").innerHTML); 如果价格有小数点 对于标签: myObj.label=documen

我有一个javascript对象和一个html表

var myObj=[
{
价格:1,,
标签:“一”
}
];

100
产品
你是说

myObj.price=parseInt(document.getElementById("price").innerHTML,10);
对于整数或

myObj.price=parseFloat(document.getElementById("price").innerHTML);
如果价格有小数点

对于标签:

myObj.label=document.getElementById("label").innerHTML;
请注意,
.innerHTML
与旧浏览器兼容-如果您不需要支持它们,请使用
.textContent

var myObj=[
{
价格:1,,
标签:“一”
}
];
window.onload=function(){
myObj.price=parseInt(document.getElementById(“price”).innerHTML,10);
myObj.label=document.getElementById(“label”).innerHTML;
window.console&&console.log(myObj)
}

100
产品

考虑到此代码:

<table>
  <tr>
    <th id = "price">100</th>
    <th id = "label">product</th>
  </tr>
<table>
见Jsfiddle

然后,要将金额转换为整数,请使用
parseInt(string)
。作为浮点值(它是一个量),使用
parseFloat(string)


您可以使用
textContent
data
wholeText
代替
nodeValue
。查看以了解更多信息并满足您的需要。

请仅就您使用“id”一事发表评论。ID是唯一的。如果要管理多行,则不应使用它们。是这样吗?
document.getElementById("price").nodeValue;
document.getElementById("label").nodeValue;