未捕获的ReferenceError:赋值中的左侧无效

时间:2011-12-05 04:39:20

标签: javascript variables

我正在尝试进行调查,每次点击都会为某个变量指定一个变量。当我点击div时,会在 span#display 中显示它。 当我运行此代码时,我得到“Uncaught ReferenceError:赋值中的左侧无效”:

var a = "none chosen";
function companyPicka()
            {
                a= "I love Valve";
            } 
            function companyPickb()
            {
                a= "I love Bethesda";
            } 
            function companyPickc()
            {
                a= "I love EA";
            } 
            function companyPickd()
            {
                a= "I love Ubisoft";
            } 
            function companyPicke()
            {
                a= "I play terrible NES, SNES games";
            } 
            function companyPickf()
            {
                a= "I like something that's so underground I wouldn't expect  you to know it";
            }
function display()
            {
                document.getElementById("display") = "a+''+b+''+c+''+d+''+e+''+f+''+g+''+h";
            }


<div>
                <span onclick="companyPicka(); this.style.border='inset';">
                Valve &#60;3</span> 
                <span onclick="companyPickb(); this.style.border='inset';"> 
                Bethesda :\ </span>
                <span onclick="companyPickc(); this.style.border='inset';">
                EA </span>
                <span onclick="companyPickd(); this.style.border='inset';"> 
                Ubisoft </span>
                <span onclick="companyPicke(); this.style.border='inset';">
                LJN</span>
                <span onclick="companyPickf(); this.style.border='inset';">
                Other</span>
            </div>

<span onclick="display();"> See Results </span>
            <div id="display">
            </div>

2 个答案:

答案 0 :(得分:4)

以下行错误:

document.getElementById("display") = "a+''+b+''+c+''+d+''+e+''+f+''+g+''+h";

您不能将字符串直接分配给HTML元素。设置元素内容的一种方法是使用.innerHTML

document.getElementById("display").innerHTML = "something";

另请注意,表达式的右侧是一个包含实际文本a+''+b+''+c+''+d+''+e+''+f+''+g+''+h的字符串 - 即字符“a”,然后是字符“+”,然后是两个撇号,等等。不是连接变量a,b,c等的表达式(不是说你的代码显示的是b,c,d等的变量)。如果删除周围的双引号,它将是连接变量的表达式。

对每个选项使用不同的函数是一种非常混乱的方式来编写这种类型的东西。你可能想尝试类似下面的东西(可以进一步整理,但我不想让你一次性过多的信息超载你):

var phrases = {
      a : "I love Valve",
      b : "I love Bethesda",
      c : "I love EA",
      d : "I love Ubisoft",
      e : "I play terrible NES, SNES games",
      f : "I like something that's so underground I wouldn't expect  you to know it"
    },
    currentPhrase = "none selected";

function companyPick(which) {
   currentPhrase = phrases[which] || "none selected";
}

function display() {
   document.getElementById("display").innerHTML = currentPhrase;
}

<div>
  <span onclick="companyPick('a'); this.style.border='inset';">
  Valve &#60;3</span> 
  <span onclick="companyPick('b'); this.style.border='inset';"> 
  Bethesda :\ </span>
  <span onclick="companyPick('c'); this.style.border='inset';">
  EA </span>
  <span onclick="companyPick('d'); this.style.border='inset';"> 
  Ubisoft </span>
  <span onclick="companyPick('e'); this.style.border='inset';">
  LJN</span>
  <span onclick="companyPick('f'); this.style.border='inset';">
  Other</span>
</div>

<span onclick="display();"> See Results </span>
<div id="display"></div>

答案 1 :(得分:3)

此行不正确:

document.getElementById("display") = "a+''+b+''+c+''+d+''+e+''+f+''+g+''+h";

使用它:

document.getElementById("display").innerHTML = a+''+b+''+c+''+d+''+e+''+f+''+g+''+h;

修改

实际上这不会产生任何场景:a+''+b+''+c+''+d+''+e+''+f+''+g+''+h

它是这样的:a+b+c+d+e+f+g+h