Javascript “为什么?”;“你摇滚”;当我单击时总是弹出警报?
我是一个新的javascript学习者。我这么做了,但不明白为什么每次我点击时都会弹出“YouRock”警报。提前谢谢Javascript “为什么?”;“你摇滚”;当我单击时总是弹出警报?,javascript,html,Javascript,Html,我是一个新的javascript学习者。我这么做了,但不明白为什么每次我点击时都会弹出“YouRock”警报。提前谢谢 函数favColors(){ var-example=document.getElementById('example'); if(document.getElementById('favorite')。value=example.options[0]。text){ 警惕(“你摇滚”); }否则{ 警惕(“没有颜色味道”); } document.getElementBy
函数favColors(){
var-example=document.getElementById('example');
if(document.getElementById('favorite')。value=example.options[0]。text){
警惕(“你摇滚”);
}否则{
警惕(“没有颜色味道”);
}
document.getElementById('favorite')。value=example.options[example.selectedIndex]。text;
}
黑色
红色
白色
粉红色
你最喜欢的运动是:
您的IF
子句需要一个=
而不是一个=
。应该是这样的:
if(document.getElementById('favorite')。value==example.options[0]。text){
而不是
if(document.getElementById('favorite')。value=example.options[0]。text){
希望能有帮助
此外,这:
document.getElementById('favorite')。value=example.options[example.selectedIndex].text;
需要是定义示例
后的第一个右语句
更新:
- 需要应用
在您可以使用它进行比较之前,请将其设置为example.options[example.selectedIndex].text
document.getElementById('favorite').value
符号用于赋值,=
用于比较。因此您的=
语句需要相应更新。请阅读IF
IF
子句需要一个=
而不是一个=
。它应该如下所示:
if(document.getElementById('favorite')。value==example.options[0]。text){
而不是
if(document.getElementById('favorite')。value=example.options[0]。text){
希望能有帮助
此外,这:
document.getElementById('favorite')。value=example.options[example.selectedIndex].text;
需要是定义示例
后的第一个右语句
更新:
- 需要应用
在您可以使用它进行比较之前,请将其设置为example.options[example.selectedIndex].text
document.getElementById('favorite').value
符号用于赋值,=
用于比较。因此您的=
语句需要相应更新。请阅读IF
- 在if语句中使用赋值而不是比较运算符
赋值运算符
//sets x to 5
x = 5
//sets y to an empty object
y = {}
//sets y to 5
y = x
正则比较运算符
//evaluates to true
5 == 5
//evaluates to true
'5' == '5'
//evaluates to true
'5' == 5
//evaluates to false
'5' == 0
严格比较运算符(应该使用的运算符)
在if语句中使用赋值而不是比较运算符 赋值运算符
//sets x to 5
x = 5
//sets y to an empty object
y = {}
//sets y to 5
y = x
正则比较运算符
//evaluates to true
5 == 5
//evaluates to true
'5' == '5'
//evaluates to true
'5' == 5
//evaluates to false
'5' == 0
严格比较运算符(应该使用的运算符)
请将代码直接包含在您的帖子中。不要将整个问题都放在标题中。请先阅读关于如何提问的帮助。如果不在此处引用,请至少提供一个代码链接。@d.popov不,仅提供一个链接是不合适的。赋值v.比较。@Dave Newton。我知道JSFIDLE是可以容忍的,而且是有原因的。Codepen也是一样的。请将代码直接包含在您的帖子中。不要将整个问题都放在标题中。请先阅读有关如何提问的帮助。如果不在此处引用,请至少提供一个代码链接。@d.popov不,仅提供一个链接是不合适的。赋值v.比较。@Dave Newton。我知道JSFIDLE是可以容忍的,这是有原因的。Codepen是samE