首页 > 代码库 > js - 02课 3

js - 02课 3

1. 自动生成5条新闻

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
<input id="btn1" type="button" value="http://www.mamicode.com/自动生成5条新闻">
<ul id="list" style="border: 1px solid red;"></ul>
<script>
    window.onload = function () {
        var dataSource = [
            ‘山西省委附近多次爆炸 官方称尚不确定是恐怖袭击‘,
            ‘甘肃张掖明令禁止转基因 书记:无力辨别只能禁止‘,
            ‘多地制定雾霾预案限行限排被批治标不治本‘,
            ‘韩媒抱怨中国雾霾侵袭韩国 称其为"黑色灾难" ‘,
            ‘伊朗革命卫队高官在叙利亚当"志愿者"被杀(图)‘
        ];
        var oBtn = document.getElementById(‘btn1‘);
        var oUl = document.getElementById(‘list‘);
        var isFive = false;

        oBtn.onclick = function () {
            if (!isFive) {
                var li = ‘‘;
                for (var i = 0; i < 5; i++) {
                    li += "<li>" + dataSource[i] + "</li>";
                }
                oUl.innerHTML = li;
                isFive = true;
            }
        }
    }
</script>
</body>
</html>

  

js - 02课 3