JavaScript操作BOM对象 - document对象; history 和 location对象 ,对话框,Date时间对象


getElementById返回拥有指定id的第一个对象(注意只是一个且是第一个)
getElementsByName返回带有指定名称的对象集合
getElementsBytagNam返回指定标签的对象集合



html代码
<div class="content" >
    <div class="logo">
<img src="images/dd_logo.jpg"><span>关闭</span>
</div>
<div class="cartList">
<ul>
<li>¥<input type="text" name="price" value="21.90"></li>
<li><input type="button" name="minus" value="-" onclick=" mins(0)"><input type="text" name="amount" value="1"><input type="button" name="plus" value="+" onclick="add(0)"></li>
<li id="price0">¥21.90</li>
<li><p>移入收藏</p><p>删除</p></li>
</ul>
<ul>
<li>¥<input type="text" name="price" value="24.00"></li>
<li><input type="button" name="minus" value="-" onclick="mins(1)"><input type="text" name="amount" value="1"><input type="button" name="plus" value="+"onclick="add(1)"></li>
<li id="price1">¥24.00</li>
<li><p>移入收藏</p><p>删除</p></li>
</ul>
<ol>
<li id="totalPrice" onclick="total()" >&nbsp;</li>
<li><span>结 算</span></li>
</ol>
</div>
</div>
JavaScrip代码
function total(){
//得到2个商品价格的集合
var prices = document.getElementsByName("price");
//得到2个数量的集合
var count = document.getElementsByName("amount");
//初始总价
var sum = 0;
//
for(var i=0;i<prices.length;i++){

sum+=prices[i].value*count[i].value;
}
//最终直接赋值
document.getElementById("totalPrice").innerHTML="¥"+sum;


history 和 location对象 
location.href返回或者设置完整url
例:<a href="javascript:location.href='spring.html'">春天</a>
点击‘春天’ 可以跳转到指定的页面 spring.html
history对象的方法
back(),forward(),go()等
例<a href="javascript:history.go(-1)">后退</a> <a href="javascript:history.go(1)">前进</a></td>
点击‘后退’时进行 页面后退,后退到上一个页面 ,前进也是一样
back()后退,forward()前进,作用与go()一样 (记不住就记 go()方法就行了
注意使用时javascript:不能省略



confirm对话框 弹出确定,取消的对话框; prompt输入对话框,alert提示对话框(这俩个就不介绍了)
例function del_plan(){
    var flag =confirm("确定要删除吗");
if(flag==true){
alert("删除成功");
}
}
上述代码点击对话框 确定 按钮之后弹出‘删除成功’,相对flase也是相似的代码




例function disptime(){
var today =new Date();

  var today =new Date();
  //年
  var year=today.getFullYear();
  //月返回0-11
  var month=today.getMonth();
  //0-6返回星期几 0是星期日
  var day=today.getDay();
  if(day==0){
  day ="日";
  }
  //日1-31
  var date=today.getDate();
  //时
var hh =today.getHours();
  //分
var mm =today.getMinutes();
  //秒
var ss =today.getSeconds();

document.getElementById("myTime").innerHTML="现在是:"+hh+":"+mm+":"+ss;
}

//进行刷新控制 每一秒进行刷新
var myTimes =setInterval("disptime()",1000);


猜你喜欢

转载自www.cnblogs.com/bincounste/p/10053984.html