源码-JavaScript&jQuery交互式前端开发-第2章-JavaScript基础指令-使用变量来存储数字

示例:使用变量来存储数字(肯能是最简单的JavaScript脚本了吧偷笑

示例效果:


JS代码如下:

// Create three variables to store the information needed.
var price;
var quantity;
var total;

// Assign values to the price and quantity variables.
price = 5;
quantity = 14;
// Calculate the total by multiplying the price by quantity.
total = price * quantity;

// Get the element with an id of cost.
var el = document.getElementById('cost');
el.textContent = '$' + total;

/* 
NOTE: textContent does not work in IE8 or earlier
You can use innerHTML, but note the security issues on p228-231
el.innerHTML = '$' + total;
*/


HTML代码如下:

<!DOCTYPE html>
<html>
  <head>
    <title>JavaScript & jQuery - Chapter 2: Basic JavaScript Instructions - Numeric Variable</title>
    <link rel="stylesheet" href="css/c02.css" />
  </head>
  <body>
    <h1>Elderflower</h1>
    <div id="content">
      <h2>Custom Signage</h2>
      <div id="cost">Cost: $5 per tile</div>
      <img src="images/preview.jpg" alt="Sign" />
    </div>
    <script src="js/numeric-variable.js"></script>
  </body>
</html>

CSS代码如下:

html {
    height: 100%;}

body {
    background-color: #b6a996;
    background: url('../images/hobart.jpg') no-repeat center center fixed;
    -webkit-background-size: cover;
    -moz-background-size: cover;
    -o-background-size: cover;
    background-size: cover;
    font-family: helvetica, arial, sans-serif;
    font-size: 100%;
    margin: 0;
    padding: 0;
    text-align: center;
    min-height: 100%;}

#content {
    background-color: #fff;
    border: 3px double #31373f;
    color: #31373f;
    display: inline-block;
    padding: 30px 50px;
    margin: 20px auto 50px auto;
    max-width: 580px;
    min-height: 4em;
    overflow: auto;
    text-align: center;}

h1, h2 {
    text-indent: 100%;
    white-space: nowrap;
    overflow: hidden;}

h1 {
    width: 278px;
    height: 112px;
    margin: 0 auto 0 auto;
    padding-top: 40px;
    background: url('../images/elderflower-logo.png') no-repeat bottom center;}

h2 {
    width: 286px;
    height: 18px;
    margin: 0 auto 20px auto;
    background: url('../images/custom-sign.png') no-repeat center center;}

#title {
    font-size: 120%;
    font-weight: bold;}

#cost, .action {
    background-color: #f84c53;
    color: #fff;
    width: auto;
    height: 1em;
    padding: 0.5em 0.75em;
    float: right;}
#cost {
    position: relative;
    top: 1em;
    right: 2em;}
.action {
    position: relative;
    top: 1.3em;
    right: -2.3em;}

#note {
    background-color: #31373f; 
    border-radius: 0.5em;
    color: #e3bb5a;
    padding: 0.5em 1em;
    margin-top: 1em;
    text-transform: uppercase;
    display: inline-block;}

a {
    color: #e3bb5a;
    text-decoration:none;}

.true, .false {
    background: url("../images/tick-cross.png");
    display: block;
    float:right;
    height: 25px;
    width: 25px;}
.false {
    background-position: 0 -25px;}

.message {
    font-weight: bold;
    line-height: 1.5em;
    height: 2em;
    display: block;
    clear: left;
    min-width: 10em;}

.message span { 
    font-weight: normal;}

.number {
    text-align: left;}

table {
    margin: 20px 100px 0 100px;}

tr td:first-child {
    font-weight: bold;
    padding-right: 20px;}

body:before, body:after {
    content: "";
    position: fixed;
    background: #fff;
    left: 0;
    right: 0;
    height: 20px;}

body:before {
    top: 0;}

body:after {
    bottom: 0;}

body {
    border-left: 20px solid #fff;
    border-right: 20px solid #fff;}





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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值