gpt4 book ai didi

javascript - 未捕获的语法错误 : Unexpected identifier while adding onmouseclick attribute using js

转载 作者:行者123 更新时间:2023-11-30 14:17:36 26 4
gpt4 key购买 nike

我不确定错误在哪里。我正在学习 JavaScript,但我一直在控制台日志中收到此错误。请帮忙。下面是代码。当我添加

时问题就来了
element.setAttribute("onmouseover","showing("+this+","+x+")");" 

build() 函数中。

当我从上面的行中删除 this 时,它起作用了。但我也想将其作为属性传递

我该如何解决这个问题?该错误一直显示在 HTML 元素中。

<!DOCTYPE html>
<html lang="en">


<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<meta http-equiv="X-UA-Compatible" content="ie=edge">

<style>

button{
background: lightblue;
font-size: 2em;
border-radius: 15px;
margin-left:auto;
margin-right: auto;
display: block;
margin-bottom: 20px;
}

.hidden{
display: none;
}
#message1{
font-size: 2em;
font-weight: bolder;
text-align :center;
margin-bottom: 20px;
}
#message2{
font-size: 2em;
font-weight: bolder;
text-align :center;
margin-bottom: 20px;
}

button:hover{
cursor: pointer;
}

.box{
height: 200px;
width: 200px;
display: inline-flex;
align-items : center;
border: 1px solid black;
margin: 5px;
text-align: center;

}

#gameArea{
text-align: center;
}
</style>

<title>Document</title>

</head>

<body>

<button id="starter" onclick="start()">Start Game</button>

<div id="message1">press start game</div>

<div id="message2">person's name will appear here.</div>

<div id="gameArea"></div>
<script>

var people=["a","b","c","d","e","f","g","h","i"]
var arr=people.slice();
var key;

function start(){
build();
shuffleArr();
// console.log(arr);
document.getElementById('starter').classList.add("hidden");
messagePass1("Find and click the names as fast you can..!!");
}

function shuffleArr(){
arr.sort(function(a,b){
return 0.5-Math.random();
});
}

function build(){
var element;
var pa;
for(var x=0;x<people.length;x++){
element=document.createElement("div");
element.setAttribute("onmouseover","showing("+this+","+x+")");
pa=document.createElement("span");
pa.innerHTML="Hidden "+(x+1);
pa.style.fontSize="2em"
pa.style.marginRight="auto";
pa.style.marginLeft="auto";
element.appendChild(pa);
element.classList.add("box");
document.getElementById("gameArea").appendChild(element);
}
}

function showing(thisHere,index){
messagePass2(index+1);
}

function messagePass1(m){
document.getElementById("message1").innerHTML=m;
}

function messagePass2(m){
document.getElementById("message2").innerHTML=m;
}
</script>

</body>

</html>

最佳答案

改变

element.setAttribute("onmouseover","showing("+this+","+x+")");

element.onmouseover = function() {showing(this,x)};

var people=["adhi","ashwin","aditya","anurag","ashwath","athira","athul","abilash","deepak"]
var arr=people.slice();
var key;

function start(){
build();
shuffleArr();
// console.log(arr);
document.getElementById('starter').classList.add("hidden");
messagePass1("Find and click the names as fast you can..!!");
}

function shuffleArr(){
arr.sort(function(a,b){
return 0.5-Math.random();
});
}

function build(){
var element;
var pa;
for(let x=0;x<people.length;x++){
element=document.createElement("div");
element.onmouseover = function() {showing(this,x)};
pa=document.createElement("span");
pa.innerHTML="Hidden "+(x+1);
pa.style.fontSize="2em"
pa.style.marginRight="auto";
pa.style.marginLeft="auto";
element.appendChild(pa);
element.classList.add("box");
document.getElementById("gameArea").appendChild(element);
}
}

function showing(thisHere,index){
messagePass2(index+1);
}

function messagePass1(m){
document.getElementById("message1").innerHTML=m;
}

function messagePass2(m){
document.getElementById("message2").innerHTML=m;
}
button{
background: lightblue;
font-size: 2em;
border-radius: 15px;
margin-left:auto;
margin-right: auto;
display: block;
margin-bottom: 20px;
}

.hidden{
display: none;
}
#message1{
font-size: 2em;
font-weight: bolder;
text-align :center;
margin-bottom: 20px;
}
#message2{
font-size: 2em;
font-weight: bolder;
text-align :center;
margin-bottom: 20px;
}

button:hover{
cursor: pointer;
}

.box{
height: 200px;
width: 200px;
display: inline-flex;
align-items : center;
border: 1px solid black;
margin: 5px;
text-align: center;

}

#gameArea{
text-align: center;
}
<button id="starter" onclick="start()">Start Game</button>

<div id="message1">press start game</div>

<div id="message2">person's name will appear here.</div>

<div id="gameArea"></div>

关于javascript - 未捕获的语法错误 : Unexpected identifier while adding onmouseclick attribute using js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53294174/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com