简体   繁体   English

Javascript / html5选择器

[英]Javascript/html5 selector

I'm trying to do the HTML5 fundamentals tutorial and this is so far my code : 我正在尝试做HTML5基础知识教程,到目前为止,这是我的代码:

<!DOCTYPE html>
<head>
    <script src="script.js"></script>
    <title>HTML Fundamentals</title>
</head>
<body>

    <div id="result" class="well"
        <div id="log">&nbsp;</div>
        <hr />
        <div class="item" id="div1">div 1</div>
        <div class="item" id="div2">div 2</div>
        <div class="item" id="div3">div 3</div>
        <div class="item" id="div4">div 4</div>
        <div class="item" id="div5">div 5</div>
        <div class="item" id="div6">div 6</div>
        <hr />
   </div>

function initiate() {

    var divs = document.querySelectorAll('div');
    var log = document.querySelector('#log');
    log.innerText = 'There are' + divs.length + 'DIVs on this page';

}

addEventListener("load", initiate);

But it does not show the message as i would like. 但是它并没有显示我想要的消息。 Care to point out what i'm missing? 要指出我想念的东西吗?

Your HTML is invalid: You're missing a > on <div id="result" class="well" . 您的HTML无效:您在<div id="result" class="well"上缺少>

Once you fix these you get an accurate result: JSFiddle demo . 解决这些问题后,您将获得准确的结果: JSFiddle演示 (6 .item div elements, 1 #log div element and 1 .well div element = 8 total). (6个.item div元素,1个#log div元件和1个.well div元素= 8个)。

You are missing an end of tag character 您缺少标签字符的结尾

 <div id="result" class="well"

should be: 应该:

 <div id="result" class="well">

otherwise it seems to work fine. 否则它似乎工作正常。 Here is a fiddle: 这是一个小提琴:

http://jsfiddle.net/B3b3N/ http://jsfiddle.net/B3b3N/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM