在js中网页面写入数据时需要注意的几点

时间:2022-02-04 02:24:51

网页代码

<tbody id="t_gun">
<s:iterator value="gunList" status="st">
<tr id="init${st.count}" class="tr_bg1" onMouseOver="this.className='tr_over'" onMouseOut="this.className='tr_bg1'" >
<td style="font-weight: bold;color: green; width:10%">
<s:property value="oilgunno"/>
</td>
<td style="font-weight: bold;color: green; width:13.5%">
<s:property value="oilname"/>
</td>
<td style="font-weight: bold;color: green; font-size:18px; width:16%;" >0</td>
<td style="font-weight: bold;color: green; font-size:18px; width:10.5%;" >0</td>
<td style="font-weight: bold;color: green; font-size:18px; width:13.5%;" >0</td>
<td style="font-weight: bold;color: green; font-size:18px; width:13.5%; " >0</td>
<td style="font-weight: bold;color: green; font-size:20px; width:12.5%;" >0</td>
<td style="font-weight: bold;color: green; width:10%;">断开</td> </tr>
</s:iterator> </tbody>

JS代码:

$("#init"+(i+1)).html('<td style="font-weight: bold;color: red; width:10%">'+obj.oilGunno+'</td>'+
'<td style="font-weight: bold;color: red; width:13.5%">'+obj.oilName+'</td>'+
'<td style="font-weight: bold;color: red; font-size:18px; width:16%;" >'+obj.cardno+'</td>'+
'<td style="font-weight: bold;color: red; font-size:18px; width:10.5%;" >'+obj.oilBalance+'</td>'+
'<td style="font-weight: bold;color: red; font-size:18px; width:13.5%;" >'+obj.oilPrice+'</td>'+
'<td style="font-weight: bold;color: red; font-size:18px; width:13.5%; " >'+obj.oilLitter+'</td>'+
'<td style="font-weight: bold;color: red; font-size:20px; width:12.5%;" >'+obj.oilAmount+'</td>'+
'<td style="font-weight: bold;color: red; width:10%;">'+obj.statusname+'</td>');

使用$("#Id").html()的方法往指定id的投入中插入一行数据,里面要注意的就是在.html()中"<td>""</td>"需要用单引号括起来,要不然JS识别不了,而且不会报错。当然用转义字符也可以,不过我闲麻烦,这个比较快;

下面值转义字符的网址,有兴趣的小伙伴可以去看看

http://tool.oschina.net/commons?type=2