[英]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.