【问题标题】:Error : document.queryselector is not a function错误:document.queryselector 不是函数
【发布时间】:2017-07-26 16:21:59
【问题描述】:

在下面的代码中,标记为 Add More 的按钮未按预期运行。理想情况下,它应该将输入值添加到页面上的无序列表中。相反,我收到以下错误:

未捕获的类型错误:document.queryselector 不是函数

页面代码

<div id="root">
    <input type="text" id="input" v-model="message"/>

    <p>The value of the input is : {{message}}</p>

    <ul>
        <li v-for="n in names" v-text="n"></li>
    </ul>

    <input id="input1" type="text"/>

    <button id="button"> Add More </button>
</div>

<script>
    var app = new Vue ({
        el : '#root',
        data : {
            message : 'Hello World!',
            favourite : 'author',
            names : ['Sunil' , 'Anis' , 'Satyajit']
        },
    });

    document.queryselector('#button').addEventListener('click', function(event) {
        var n = document.queryselector('#input1');
        app.names.push(n.value);
        n.value = '';
    });

</script>

【问题讨论】:

    标签: button vuejs2 document selectors-api mounted-volumes


    【解决方案1】:

    它的document.querySelector。注意querySelector 中的大写 S。

    您的addEventListner 也有拼写错误。应该是addEventListener

    【讨论】:

    • 谢谢。我已经纠正了。还是不行。
    • @SFDC_Learner 是否愿意对此进行扩展?你还是得到 querySelector is not a function?
    • 请发现我已经更新了删除已安装部分的代码。它仍然无法正常工作,并显示以下错误。
    • @SFDC_Learner 您使用该方法两次,如果您要同时替换它们,它应该可以解决您的问题。
    • @SFDC_Learner 您在代码的其他部分仍然有带有小写 S 的查询选择器。
    猜你喜欢
    • 1970-01-01
    • 2020-09-22
    • 1970-01-01
    • 2017-10-23
    • 2013-01-04
    • 1970-01-01
    • 2017-05-09
    • 2021-09-14
    • 2021-05-10
    相关资源
    最近更新 更多