源码-JavaScript&jQuery交互式前端开发-第2章-JavaScript基础指令-章节示例


示例效果:


JS代码:

// Create variables for the welcome message
var greeting = '您好, ';
var name = '黄先生';
var message = ',您的结账单如下:';
// Concatenate the three variables above to create the welcome message
var welcome = greeting + name + message;

// Create variables to hold details about the sign
var sign = '希尔顿大酒店';
var tiles = sign.length;
var subTotal = tiles * 5;
var shipping = 7;
var grandTotal = subTotal + shipping;

// Get the element that has an id of greeting
var el = document.getElementById('greeting');
// Replace the content of that element with the personalized welcome message
el.textContent = welcome;

// Get the element that has an id of userSign then update its contents
var elSign = document.getElementById('userSign');
elSign.textContent = sign;

// Get the element that has an id of tiles then update its contents
var elTiles = document.getElementById('tiles');
elTiles.textContent = tiles;

// Get the element that has an id of subTotal then update its contents
var elSubTotal = document.getElementById('subTotal');
elSubTotal.textContent = '$' + subTotal;

// Get the element that has an id of shipping then update its contents
var elShipping = document.getElementById('shipping');
elShipping.textContent = '$' + shipping;

// Get the element that has an id of grandTotal then update its contents
var elGrandTotal = document.getElementById('grandTotal');
elGrandTotal.textContent = '$' + grandTotal;

/* 
NOTE: textContent does not work in IE8 or earlier
You can use innerHTML instead of textContent, but note the security issues on p228-231

In the first print run, line 33-34 repeated elSubTotal (rather than elShipping).
This was fixed in later print runs and in this code sample.
*/


HTML代码:

<html>
<head>
<meta content="text/html; charset=utf-8"/>
  <title>JavaScript & jQuery - Chapter 2: Basic JavaScript Instructions - Example</title>
  <link rel="stylesheet" href="css/c02.css" />
</head>
<body>
  <h1>Elderflower</h1>
  <div id="content">
    <div id="greeting" class="message">Hello!</div>
    <table>
      <tr>
        <td>Custom sign: </td>
        <td id="userSign"></td>
      </tr>
      <tr>
        <td>Total tiles: </td>
        <td id="tiles"></td>
      </tr>
      <tr>
        <td>Subtotal: </td>
        <td id="subTotal">$</td>
      </tr>
      <tr>
        <td>Shipping: </td>
        <td id="shipping">$</td>
      </tr>
      <tr>
        <td>Grand total: </td>
        <td id="grandTotal">$</td></tr>
    </table>
    <a href="#" class="action">Pay Now</a>
  </div>
  <script src="js/example.js"></script>
</body>
</html>

CSS代码:(参考:源码-JavaScript&jQuery交互式前端开发-第2章-JavaScript基础指令-使用变量来存储数字,http://blog.csdn.net/hpdlzu80100/article/details/52669264


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值