笔记练习:《Javascript入门经典(第5版)》page175_14.11_Practice

对程序清单 14.2 和14.3 的代码做进一步修改,让没有开启 Javascript 功能的用户只能看到 <noscript> 标签里的内容,看不到额外的文本的按钮。(提示:利用 innerHTML 或 DOM 方法把这些元素写入页面。)


首先列出原代码( html 和 js 文件),再列出解决方法的代码。

原 HTML 代码如下:

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>Current Date and Time</title>
    <style type="text/css">
        p{
            font:14px normal arial,verdana,helvetica;
        }
    </style>
    <script src="datetime(old).js"></script>
</head>
<body>
The current date and time are:<br/>
<div id="div1"></div>
<input id="btn1" type="button" value="Refresh" />
<noscript>
    <h1>Your browser dose not support JavaScript.</h1>
    <p>Please consult your computer's operating system for local date and time information or click
        <a href="clock.php" target="_blank">HERE</a> to read the server time.</p>
</noscript>
</body>
</html>

原 JS 代码如下:

function telltime(){
    var out = "";
    var now = new Date();
    out += "<br />Date:" + now.getDate();
    out += "<br />Month:" + now.getMonth();
    out += "<br />Year" + now.getFullYear();
    out += "<br />Hours:" + now.getHours();
    out += "<br />Minutes:" + now.getMinutes();
    out += "<br />Seconds:" + now.getSeconds();
    document.getElementById("div1").innerHTML = out;
}
window.onload = function(){
    document.getElementById("btn1").onclick = function(){
        location.reload();
    }
    telltime();
}



通过测试并解决方案的代码

新的 HTML 代码如下:(只保留 <noscript> 标签 的内容,并需要为  <noscript>  和  <body> 添加 “id”属性。)

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>Current Date and Time</title>
    <style type="text/css">
        p{
            font:14px normal arial,verdana,helvetica;
        }
    </style>
    <script src="datetime.js"></script>
</head>
<body id="BODY">
    <noscript id="noS">
        <h1>Your browser dose not support JavaScript.</h1>
        <p>Please consult your computer's operating system for local date and time information or click
            <a href="clock.php" target="_blank">HERE</a> to read the server time.</p>
    </noscript>
</body>
</html>


新的 JS 代码如下:

function telltime(){
    var out = "";
    var now = new Date();
    out += "<br />Date:" + now.getDate();
    out += "<br />Month:" + now.getMonth();
    out += "<br />Year" + now.getFullYear();
    out += "<br />Hours:" + now.getHours();
    out += "<br />Minutes:" + now.getMinutes();
    out += "<br />Seconds:" + now.getSeconds();
    var newP = document.createElement("p").appendChild(document.createTextNode("The current date and time are:"));
    document.getElementById("BODY").innerHTML = newP.textContent + "<br/>" + out + "<br/>";
}
window.onload = function(){
    var newBtn = document.createElement("input");
    newBtn.setAttribute("type","button");
    newBtn.setAttribute("value","Refresh");
    newBtn.addEventListener('click',function(){location.reload()});
    var noScript = document.getElementById("noS");
    telltime();
    document.body.insertBefore(newBtn,noScript.nextSibling);

}

最后,当关闭浏览器的 JavaScript 脚本功能时,只显示 <noscript> 的内容,当开启浏览器的 JavaScript 脚本功能时,修改后的代码与原代码实现效果基本保持一致。




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值