JavaScript
-
Javascript : 客户端的一个脚本语言 -
js是一门弱类型语言,变量的数据类型由后面赋的值决定 -
数据类型:
- 基本数据类型:数值,字符,布尔。。。
- 引用数据类型:new 出来的对象,数组。。
-
js的定义方法类似于python,没有返回值类型,没有参数类型,也不确定有没有返回值 function hello(num1,num2,name) {
if(num1>num2){
return "hello to"+name;
}else{
alert("Hello")
}
}
<script language="JavaScript">
var str = "hello world";
alert(str)
var persion = new Object();
persion.pid = "p001";
persion.pname = "xiaowang"
alert(persion.pname)
function hello1(name){
return "hello to"+name;
}
function hello(num1,num2,name) {
if(num1>num2){
return "hello to"+name;
}else{
alert("Hello")
}
}
</script>
初学时候,html中会混杂着和JS的绑定,代码如下:
涉及到的css在上一章
javascript-01.js
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代码
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="css-demo02.css">
<script type="text/javascript" src="javascript-01.js"></script>
</head>
<body>
<div id="div_container">
<div id="div_fruit_list">
<table id="tbl_fruit">
<tr>
<th>名称</th>
<th>单价</th>
<th>数量</th>
<th>小计</th>
<th>操作</th>
</tr>
<tr onmouseover="showBGColor()" onmouseout="clearBGColor()">
<td>苹果</td>
<td onmouseover="showHand()">100</td>
<td>5</td>
<td>500</td>
<td>删除</td>
</tr>
<tr onmouseover="showBGColor()" onmouseout="clearBGColor()">
<td>香蕉</td>
<td onmouseover="showHand()">100</td>
<td>5</td>
<td>500</td>
<td>删除</td>
</tr>
<tr onmouseover="showBGColor()" onmouseout="clearBGColor()">
<td>鸭梨</td>
<td onmouseover="showHand()">100</td>
<td>5</td>
<td>500</td>
<td>删除</td>
</tr>
<tr onmouseover="showBGColor()" onmouseout="clearBGColor()">
<td>西瓜</td>
<td onmouseover="showHand()">100</td>
<td>5</td>
<td>500</td>
<td>删除</td>
</tr>
<tr>
<td>总计</td>
<td colspan="4">2500</td>
</tr>
</table>
</div>
</div>
</body>
</html>
接下来对上面的进行优化,使html中仅有html代码 去掉多余的绑定
- 去掉html文件中的绑定事件
- 在JS中增加如下代码:
window.onload=function (){
var fruitTbl = document.getElementById("tbl_fruit");
var rows = fruitTbl.rows;
for(var i=0;i<rows.length;i++){
var tr = rows[i];
tr.onmouseover=showBGColor;
tr.onmouseout=clearBGColor;
var cells = tr.cells;
var priceID = cells[1];
priceID.onmouseover=showHand;
}
}
更新单价
priceID.onclick=editPrice;
function editPrice(){
if(event && event.srcElement && event.srcElement.tagName=="TD"){
var priceTD = event.srcElement;
if(priceTD.firstChild && priceTD.firstChild.nodeType==3) {
var oldPirce = priceTD.innerText;
priceTD.innerHTML="<input type='text' size='4'/>"
var input = priceTD.firstChild;
if(input.tagName=="INPUT"){
input.value = oldPirce;
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;
}
删除某条记录操作
var img = cells[4].firstChild;
if(img && img.tagName=="IMG"){
img.onclick=delFruit;
}
在editPrice()中加入如下
input.onkeydown=ckInput;
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 ckInput(){
var kc = event.keyCode;
if(!((kc>=48 && kc<=57) || kc==8 || kc==13)){
event.returnValue=false;
}
if(kc==13){
event.srcElement.blur();
}
}
|