<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="css/demo05.css">
<script type="text/javascript" src="js/demo07.js"></script>
</head>
<body>
<div id="div_container">
<div id="div_fruit_list">
<table id="tbl_fruit">
<tr>
<th class="w20">名称</th>
<th class="w20">单价</th>
<th class="w20">数量</th>
<th class="w20">小计</th>
<th>操作</th>
</tr>
<tr onmouseover="showBGColor()" onmouseout="clearBGColor()">
<td>苹果</td>
<td onmouseover="showHand()">5</td>
<td>20</td>
<td>100</td>
<td><img src="imgs/del.jpg" class="delImg"/></td>
</tr>
<tr onmouseover="showBGColor()" onmouseout="clearBGColor()">
<td>西瓜</td>
<td onmouseover="showHand()">3</td>
<td>20</td>
<td>60</td>
<td><img src="imgs/del.jpg" class="delImg"/></td>
</tr>
<tr onmouseover="showBGColor()" onmouseout="clearBGColor()">
<td>菠萝</td>
<td onmouseover="showHand()">4</td>
<td>25</td>
<td>100</td>
<td><img src="imgs/del.jpg" class="delImg"/></td>
</tr>
<tr onmouseover="showBGColor()" onmouseout="clearBGColor()">
<td>榴莲</td>
<td onmouseover="showHand()">3</td>
<td>30</td>
<td>90</td>
<td><img src="imgs/del.jpg" class="delImg"/></td>
</tr>
<tr>
<td>总计</td>
<td colspan="4">999</td>
</tr>
</table>
</div>
</div>
</body>
</html>
function showBGColor(){
if(event && event.srcElement && event.srcElement.tagName=="TD"){
var td = event.srcElement ;
var tr = td.parentElement ;
tr.style.backgroundColor = "navy" ;
var tds = tr.cells;
for(var i = 0 ; i<tds.length ; i++){
tds[i].style.color="white";
}
}
}
function clearBGColor(){
if(event && event.srcElement && event.srcElement.tagName=="TD"){
var td = event.srcElement ;
var tr = td.parentElement ;
tr.style.backgroundColor="transparent";
var tds = tr.cells;
for(var i = 0 ; i<tds.length ; i++){
tds[i].style.color="threeddarkshadow";
}
}
}
function showHand(){
if(event && event.srcElement && event.srcElement.tagName=="TD"){
var td = event.srcElement ;
td.style.cursor="hand";
}
}
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="css/demo05.css">
<script type="text/javascript" src="js/demo08.js"></script>
</head>
<body>
<div id="div_container">
<div id="div_fruit_list">
<table id="tbl_fruit">
<tr>
<th class="w20">名称</th>
<th class="w20">单价</th>
<th class="w20">数量</th>
<th class="w20">小计</th>
<th>操作</th>
</tr>
<tr>
<td>苹果</td>
<td>5</td>
<td>20</td>
<td>100</td>
<td><img src="imgs/del.jpg" class="delImg"/></td>
</tr>
<tr>
<td>西瓜</td>
<td>3</td>
<td>20</td>
<td>60</td>
<td><img src="imgs/del.jpg" class="delImg"/></td>
</tr>
<tr>
<td>菠萝</td>
<td>4</td>
<td>25</td>
<td>100</td>
<td><img src="imgs/del.jpg" class="delImg"/></td>
</tr>
<tr>
<td>榴莲</td>
<td>3</td>
<td>30</td>
<td>90</td>
<td><img src="imgs/del.jpg" class="delImg"/></td>
</tr>
<tr>
<td>总计</td>
<td colspan="4">999</td>
</tr>
</table>
</div>
</div>
</body>
</html>
window.onload=function(){
updateZJ();
var fruitTbl = document.getElementById("tbl_fruit");
var rows = fruitTbl.rows ;
for(var i = 1 ; i<rows.length-1 ; i++){
var tr = rows[i];
tr.onmouseover=showBGColor;
tr.onmouseout=clearBGColor;
var cells = tr.cells;
var priceTD = cells[1];
priceTD.onmouseover = showHand ;
priceTD.onclick=editPrice;
}
}
function editPrice(){
if(event && event.srcElement && event.srcElement.tagName=="TD"){
var priceTD = event.srcElement ;
if(priceTD.firstChild && priceTD.firstChild.nodeType==3){
var oldPrice = priceTD.innerText ;
priceTD.innerHTML="<input type='text' size='4'/>";
var input = priceTD.firstChild;
if(input.tagName=="INPUT"){
input.value = oldPrice ;
input.select();
input.onblur=updatePrice ;
}
}
}
}
function updatePrice(){
if(event && event.srcElement && event.srcElement.tagName=="INPUT"){
var input = event.srcElement ;
var newPrice = input.value ;
var priceTD = input.parentElement ;
priceTD.innerText = newPrice ;
updateXJ(priceTD.parentElement);
}
}
function updateXJ(tr){
if(tr && tr.tagName=="TR"){
var tds = tr.cells;
var price = tds[1].innerText ;
var count = tds[2].innerText ;
var xj = parseInt(price) * parseInt(count);
tds[3].innerText = xj ;
updateZJ();
}
}
function updateZJ(){
var fruitTbl = document.getElementById("tbl_fruit");
var rows = fruitTbl.rows ;
var sum = 0 ;
for(var i = 1; i<rows.length-1 ; i++){
var tr = rows[i];
var xj = parseInt(tr.cells[3].innerText);
sum = sum + xj ;
}
rows[rows.length-1].cells[1].innerText = sum ;
}
function showBGColor(){
if(event && event.srcElement && event.srcElement.tagName=="TD"){
var td = event.srcElement ;
var tr = td.parentElement ;
tr.style.backgroundColor = "navy" ;
var tds = tr.cells;
for(var i = 0 ; i<tds.length ; i++){
tds[i].style.color="white";
}
}
}
function clearBGColor(){
if(event && event.srcElement && event.srcElement.tagName=="TD"){
var td = event.srcElement ;
var tr = td.parentElement ;
tr.style.backgroundColor="transparent";
var tds = tr.cells;
for(var i = 0 ; i<tds.length ; i++){
tds[i].style.color="threeddarkshadow";
}
}
}
function showHand(){
if(event && event.srcElement && event.srcElement.tagName=="TD"){
var td = event.srcElement ;
td.style.cursor="hand";
}
}
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="css/demo05.css">
<script type="text/javascript" src="js/demo09.js"></script>
</head>
<body>
<div id="div_container">
<div id="div_fruit_list">
<table id="tbl_fruit">
<tr>
<th class="w20">名称</th>
<th class="w20">单价</th>
<th class="w20">数量</th>
<th class="w20">小计</th>
<th>操作</th>
</tr>
<tr>
<td>苹果</td>
<td>5</td>
<td>20</td>
<td>100</td>
<td><img src="imgs/del.jpg" class="delImg"/></td>
</tr>
<tr>
<td>西瓜</td>
<td>3</td>
<td>20</td>
<td>60</td>
<td><img src="imgs/del.jpg" class="delImg"/></td>
</tr>
<tr>
<td>菠萝</td>
<td>4</td>
<td>25</td>
<td>100</td>
<td><img src="imgs/del.jpg" class="delImg"/></td>
</tr>
<tr>
<td>榴莲</td>
<td>3</td>
<td>30</td>
<td>90</td>
<td><img src="imgs/del.jpg" class="delImg"/></td>
</tr>
<tr>
<td>总计</td>
<td colspan="4">999</td>
</tr>
</table>
<hr/>
<div id="add_fruit_div">
<table>
<tr>
<td>名称:</td>
<td><input type='text' id='fname'/></td>
</tr>
<tr>
<td>单价:</td>
<td><input type='text' id='price'/></td>
</tr>
<tr>
<td>数量:</td>
<td><input type='text' id='fcount'/></td>
</tr>
<tr>
<th colspan="2">
<input type='button' class="btn" value="添加"/>
<input type='button' class="btn" value="重填"/>
</th>
</tr>
</table>
</div>
</div>
</div>
</body>
</html>
window.onload=function(){
updateZJ();
var fruitTbl = document.getElementById("tbl_fruit");
var rows = fruitTbl.rows ;
for(var i = 1 ; i<rows.length-1 ; i++){
var tr = rows[i];
tr.onmouseover=showBGColor;
tr.onmouseout=clearBGColor;
var cells = tr.cells;
var priceTD = cells[1];
priceTD.onmouseover = showHand ;
priceTD.onclick=editPrice;
var img = cells[4].firstChild;
if(img && img.tagName=="IMG"){
img.onclick = delFruit ;
}
}
}
function delFruit(){
if(event && event.srcElement && event.srcElement.tagName=="IMG"){
if(window.confirm("是否确认删除当前库存记录")){
var img = event.srcElement ;
var tr = img.parentElement.parentElement ;
var fruitTbl = document.getElementById("tbl_fruit");
fruitTbl.deleteRow(tr.rowIndex);
updateZJ();
}
}
}
function editPrice(){
if(event && event.srcElement && event.srcElement.tagName=="TD"){
var priceTD = event.srcElement ;
if(priceTD.firstChild && priceTD.firstChild.nodeType==3){
var oldPrice = priceTD.innerText ;
priceTD.innerHTML="<input type='text' size='4'/>";
var input = priceTD.firstChild;
if(input.tagName=="INPUT"){
input.value = oldPrice ;
input.select();
input.onblur=updatePrice ;
input.onkeydown=ckInput;
}
}
}
}
function ckInput(){
var kc = event.keyCode ;
if(!( ( kc>=48 && kc<=57 ) || kc==8 || kc==13 )){
event.returnValue=false;
}
if(kc==13){
event.srcElement.blur();
}
}
function updatePrice(){
if(event && event.srcElement && event.srcElement.tagName=="INPUT"){
var input = event.srcElement ;
var newPrice = input.value ;
var priceTD = input.parentElement ;
priceTD.innerText = newPrice ;
updateXJ(priceTD.parentElement);
}
}
function updateXJ(tr){
if(tr && tr.tagName=="TR"){
var tds = tr.cells;
var price = tds[1].innerText ;
var count = tds[2].innerText ;
var xj = parseInt(price) * parseInt(count);
tds[3].innerText = xj ;
updateZJ();
}
}
function updateZJ(){
var fruitTbl = document.getElementById("tbl_fruit");
var rows = fruitTbl.rows ;
var sum = 0 ;
for(var i = 1; i<rows.length-1 ; i++){
var tr = rows[i];
var xj = parseInt(tr.cells[3].innerText);
sum = sum + xj ;
}
rows[rows.length-1].cells[1].innerText = sum ;
}
function showBGColor(){
if(event && event.srcElement && event.srcElement.tagName=="TD"){
var td = event.srcElement ;
var tr = td.parentElement ;
tr.style.backgroundColor = "navy" ;
var tds = tr.cells;
for(var i = 0 ; i<tds.length ; i++){
tds[i].style.color="white";
}
}
}
function clearBGColor(){
if(event && event.srcElement && event.srcElement.tagName=="TD"){
var td = event.srcElement ;
var tr = td.parentElement ;
tr.style.backgroundColor="transparent";
var tds = tr.cells;
for(var i = 0 ; i<tds.length ; i++){
tds[i].style.color="threeddarkshadow";
}
}
}
function showHand(){
if(event && event.srcElement && event.srcElement.tagName=="TD"){
var td = event.srcElement ;
td.style.cursor="hand";
}
}