简体   繁体   English

我的代码有错误还是我只是写错了?

[英]Is there an error in my code or have I simply written it wrong?

I'm making a code where if you press the button after the name it will move to the other list.我正在编写一个代码,如果您在名称后按下按钮,它将移动到另一个列表。 Pressing a button give me the error: "missing ) after argument list".按下按钮给我错误:“参数列表后缺少)”。 I can't seem to find anything wrong in the code.我似乎在代码中找不到任何错误。

<!DOCTYPE html>
<html>
    <title>Favoritter</title>
    <body>
        <p>Hotell</p>
        <p id="hotellDiv"></p>
        <p>Favoritter</p>
        <p id="favDiv"></p>
    </body>
    <script>
        let hotelliste = ["Norwegian Wild", "Stofjord Hotel", "Norefjell Ski og Spa", "Brikdalsbre Fjellstove", "Gudvangen Fjordtell"];
        let favoritter = [];
        skrivhliste();
        skrivfliste();
        
        function skrivhliste(){
            document.getElementById("hotellDiv").innerHTML = "";
            for (var j = 0; j < hotelliste.length; j++){
                document.getElementById("hotellDiv").innerHTML += hotelliste[j] + "<input type=\"button\" onclick=\"leggTil("+hotelliste[j]+")\"><br>";
            }
        }
        function skrivfliste(){
            document.getElementById("favDiv").innerHTML = "";
            for (var j = 0; j < favoritter.length; j++){
                document.getElementById("favDiv").innerHTML += favoritter[j] + "<input type=\"button\" onclick=\"fjern("+favoritter[j]+")\"><br>";
            }
        }
        
        function leggTil(hotell){
            if (hotelliste.indexOf(hotell) > -1) {
                hotelliste.splice(hotelliste.indexOf(hotell), 1);
            }
            favoritter.push(hotell);
            skrivhliste();
        }
        function fjern(hotell){
            if (favoritter.indexOf(hotell) > -1) {
                favoritter.splice(favoritter.indexOf(hotell), 1);
            }
            hotelliste.push(hotell);
            skrivfliste();
        }
    </script>
</html>

Look at this:看这个:

 "<input type=\"button\" onclick=\"fjern("+favoritter[j]+")\">

What string are you going to end up with when you insert the value of favoritter[j] ?当您插入favoritter[j]的值时,您最终会得到什么字符串?

 <input type="button" onclick="fjern(Norwegian Wild)">

There you don't have the string "Norwegian Wild" , you have the variable Norwegian followed by a space followed by the variable Wild (and neither of those variables exist).在那里你没有字符串"Norwegian Wild" ,你有变量Norwegian后跟一个空格,后跟变量Wild (并且这些变量都不存在)。

If you are programatically generating JavaScript then you need to generate the quotes that go around strings you generate.如果您以编程方式生成 JavaScript 那么您需要在您生成的字符串周围生成 go 的引号。

This is hard to do well.这很难做好。 Especially when that JS gets embedded in HTML that you are also generating on the fly.尤其是当 JS 嵌入到 HTML 中时,您也在动态生成。 You have multiple levels of escape sequences to deal with.您有多个级别的转义序列要处理。

Avoid generating strings like this.避免生成这样的字符串。 Use direct DOM methods instead.请改用直接 DOM 方法。

For example:例如:

Once, so it can be reused:一次,因此可以重复使用:

function clickHandler(event) {
    const button = event.currentTarget;
    const hotel = button.dataset.hotel;
    leggTil(hotel);
}

Then inside your loop:然后在你的循环中:

const button = document.createElement('input');
button.type = 'button';
button.value = 'display label';
button.dataset.hotel = hotelliste[j];
button.addEventListener('click', clickHandler);
document.getElementById("hotellDiv").appendChild(button);

Your code is ok, please make a string into onclick function like below.您的代码没问题,请像下面这样将字符串转换为 onclick function。 Pass the value in single quotes into both onclick function.将单引号中的值传递到 onclick function 中。

document.getElementById("favDiv").innerHTML += favoritter[j] + "<input type=\"button\" onclick=\"fjern('"+favoritter[j]+"')\"><br>";

暂无
暂无

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

相关问题 我在我的 JS 代码中编写了 fadeOut 命令,在我的待办事项列表项目中,但是淡出不能正常工作并在谷歌控制台中显示错误? - I have written fadeOut command in my JS code, in my to do list project but the fadeout is not working properly and showing error in google console? 这些是我为我编写的代码,当用户单击模态之外的任何位置时,请关闭它,但不起作用。我的代码有什么问题 - these my code i written for When the user clicks anywhere outside of the modal, close it but it is not working .what is the wrong in my code 我在代码中做错了什么? 它显示了意外的令牌错误 - What have I done wrong in my code? It shows an Unexpected token error 我的JavaScript代码正常运行,但我有错误。 - My JavaScript code is working , but I have error . 我已经编写了从框中复制链接的代码,但它是否向我显示以下错误 - I have written code to copy the link from box but is it showing me the following error 我已经编写了 JavaScript 代码来获取某一天的讲座,但我不知道如何将我的 jQuery 链接到 JavaScript - I have written JavaScript code to fetch lectures of a particular day but I am not getting how to link my jQuery to JavaScript 如何运行我用JavaScript编写的代码? - How do I run code I have written in JavaScript? 在 Google 表格中,我的侧边栏中有一个未提交数据的表单。 我的代码有什么问题? - In Google sheets I have a form in my sidebar that is not submitting the data. What is wrong with my code? 我写的代码没有显示输入百分比的结果 - The code i have written doesnt show results on input percentage 我已经编写了此代码,但未显示任何输出。 为什么会这样呢? - I have written this code but it is not showing any outputs. Why is this happening?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM