JavaScript创建简单购物车对象

来源:互联网 发布:淘宝男童衣服哪家好 编辑:程序博客网 时间:2024/04/28 02:44

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>


<script type="text/javascript">
    var bookArray = new Array();
 var count = 0;
 //混合构造函数方法创建书籍对外象
 function Book(bookname)
 {
  this.bookname = bookname;
 }

 Book.prototype.getBookName=function(){
  return this.bookname;
 }
 //混合原型、构造函数方法创建书籍对外象
 function ShopCart(arg)
 {
  this.book = arg;
 }
 ShopCart.prototype.say = function (count)
 {
  bookArray[count] = this.book.getBookName();
 }
 ShopCart.prototype.write = function (count)
 {
  var name = "";
 
  for(var i=0;i<bookArray.length;i++)
  {
   var name = name + bookArray[i] + "/n";
  }
  alert("你已经购买的书籍是:"+name);
 }
 //事件处理程序
 function buy(bookid)
 {
  var bookobj = document.getElementById(bookid);
  var name = bookobj.innerHTML;
  var shopCart = new ShopCart(new Book(name));
  shopCart.say(count);
  count = count + 1;
  shopCart.write();
 }
</script>
</head>

<body>
<table border="1">
 <tr><td>作者</td><td>书名</td><td>出版社</td><td>状态</td></tr>
 <tr><td><p id="book1">Javascript应用程序经典实例</p></td><td>Jeerry Bradebnbaug</td><td>中国出版社</td>
 <td><input type="button" value="购买" onclick="buy('book1')"/></td></tr>
 <tr><td><p id="book2">Javascript学习宝典</p></td><td>DAVA Bradebnbaug</td><td>中国出版社</td>
 <td><input type="button" value="购买" onclick="buy('book1')"/></td></tr>
</table>
</body>
</html>