Javascript设置属性值错误:";赋值中的左侧无效“;
我正在学习使用javascript,并尝试在每次点击后在“bruh”和“moment”之间交替使用一些文本。我试图将文本的状态存储在属性“数据状态”中,该属性(应该)在1和2之间交替,但我在第12行得到了“未捕获的引用错误:赋值中的左侧无效”Javascript设置属性值错误:";赋值中的左侧无效“;,javascript,html,css,Javascript,Html,Css,我正在学习使用javascript,并尝试在每次点击后在“bruh”和“moment”之间交替使用一些文本。我试图将文本的状态存储在属性“数据状态”中,该属性(应该)在1和2之间交替,但我在第12行得到了“未捕获的引用错误:赋值中的左侧无效” <html> <head> <title>bruh moment</title> <style type="text/css"> * {text-align: ce
<html>
<head>
<title>bruh moment</title>
<style type="text/css">
* {text-align: center; color: blue;}
</style>
<script type="text/javascript">
function funcClick(){
var myParagraph = document.getElementById("myp");
if (myParagraph.data-state == 1){
myParagraph.innerHTML = "bruh";
myParagraph.data-state = 2; //ERROR IS HERE
}else{
myParagraph.innerHTML = "click again i dare you";
myParagraph.data-state = 1;
};
};
</script>
</head>
<body>
<p id="myp"; data-state=1;> bruh </p>
<button type="button" onclick="funcClick()">
Click me
</button>
</body>
</html>
布鲁时刻
*{文本对齐:居中;颜色:蓝色;}
函数funclick(){
var my段落=document.getElementById(“myp”);
如果(MyParagation.data-state==1){
myparagration.innerHTML=“bruh”;
MyParagation.data-state=2;//此处有错误
}否则{
myparagration.innerHTML=“再次单击我敢打赌”;
MyParagation.data-state=1;
};
};
布鲁
点击我
预期:
单击按钮->“时刻”
单击按钮->“bruh”
单击按钮->“时刻”
单击按钮->“bruh”
实际:
[未捕获引用错误:赋值中的左侧无效]
单击按钮->“bruh”
[未捕获引用错误:未定义funcClick]
单击按钮->“bruh”
[未捕获引用错误:未定义funcClick]
以下是实际的网页,以防有帮助您不能在变量名中使用连字符
-
-使用[]
括号:
myParagraph["data-state"] = 2;