Bil*_*ill 2 html javascript object
我有一个javascript对象和一个html表.
var myObj = [
{
price: 1,
label: "One"
}
];Run Code Online (Sandbox Code Playgroud)
<table>
<tr>
<th id = "price">100</th>
<th id = "label">product</th>
</tr>
<table>Run Code Online (Sandbox Code Playgroud)
是否可以在对象中插入表值?
为了拥有更新的属性
{
price: 100,
label: "product"
}
Run Code Online (Sandbox Code Playgroud)
你的意思是
myObj.price=parseInt(document.getElementById("price").innerHTML,10);
Run Code Online (Sandbox Code Playgroud)
对于整数或
myObj.price=parseFloat(document.getElementById("price").innerHTML);
Run Code Online (Sandbox Code Playgroud)
如果价格有小数
并为标签:
myObj.label=document.getElementById("label").innerHTML;
Run Code Online (Sandbox Code Playgroud)
请注意,.innerHTML它与旧版浏览器兼容 - 如果您不需要支持它们,请.textContent改为使用
喜欢
var myObj = [
{
price: 1,
label: "One"
}
];
window.onload=function() {
myObj.price=parseInt(document.getElementById("price").innerHTML,10);
myObj.label=document.getElementById("label").innerHTML;
window.console&&console.log(myObj)
}Run Code Online (Sandbox Code Playgroud)
<table>
<tr>
<th id = "price">100</th>
<th id = "label">product</th>
</tr>
<table>Run Code Online (Sandbox Code Playgroud)