const ddk = document.querySelector('#ddk> input');不起作用?只是在控制台中显示null

时间:2019-03-16 09:12:59

标签: javascript html converters

我正在尝试制作货币转换器,但querySelector为空,我不知道如何解决。

HTML:

<html>
    <head>
        <meta charset="utf-8">
        <script type="text/javascript" src="valuta.js"></script>
        <link rel="stylesheet" type="text/css" href="valuta.css">
    </head>
    <body>
        <div class="ddk">
            <input type="number" placeholder="DDK">
        </div>  

        <div id="usd">
            <input type="number" placeholder="DDK">
        </div>  

        <div id="eur">
            <input type="number" placeholder="DDK">
        </div>

    </body>
</html>

JS:

const ddk = document.querySelector('#ddk > input');
const usd = document.querySelector('#usd > input');
const eur = document.querySelector('#eur > input');

console.log(ddk);

1 个答案:

答案 0 :(得分:1)

const ddk = document.querySelector('.ddk > input');

#匹配ID,.匹配类别。

const ddk = document.querySelector('.ddk > input');
console.log(ddk);
<html>
    <head>
        <meta charset="utf-8">
        <script type="text/javascript" src="valuta.js"></script>
        <link rel="stylesheet" type="text/css" href="valuta.css">
    </head>
    <body>
        <div class="ddk">
            <input type="number" placeholder="DDK">
        </div>  

        <div id="usd">
            <input type="number" placeholder="DDK">
        </div>  

        <div id="eur">
            <input type="number" placeholder="DDK">
        </div>

    </body>
</html>