Javascript字符串包含“”无法匹配

时间:2017-03-08 12:40:18

标签: javascript html

我在HTML中有一个文本必须在文本中包含&nbsp,然后我使用Javascript来获取innerHTML并进行比较,但它不起作用。

   var x = randomGen();
   document.getElementById("c"+x[0]).innerHTML="&nbsp&nbspa, b, c&nbsp&nbsp";
   document.getElementById("c"+x[1]).innerHTML="&nbsp&nbspc, d, e&nbsp&nbsp";
   document.getElementById("c"+x[2]).innerHTML="&nbsp&nbspf, g, h&nbsp&nbsp";
   document.getElementById("c"+x[3]).innerHTML="&nbsp&nbspa, a, a&nbsp&nbsp";

function checkans(ans){
    if (ans=="&nbsp&nbspa, a, a&nbsp&nbsp") {
       alert(ans);
    }
}

HTML正文:

<div class="ans" id="c1" onclick="checkans(this.innerText)"></div>
<div class="ans" id="c2" onclick="checkans(this.innerText)"></div>
<div class="ans" id="c3" onclick="checkans(this.innerText)"></div>
<div class="ans" id="c4" onclick="checkans(this.innerText)"></div>

所有案例都返回false。

1 个答案:

答案 0 :(得分:4)

  

然后我使用Javascript来获取innerHTML并进行比较

事实并非如此。您非常清楚地使用innerText,而不是innerHTML,这些不一样。

如果您实际使用innerHTML,并更正您的代码以使用&nbsp;(当您将&nsbp分配给&nsbp;时,浏览器会自动将.innerHTML更改为var x = [3, 2, 1, 4] document.getElementById("c" + x[0]).innerHTML = "&nbsp;&nbsp;a, b, c&nbsp;&nbsp;"; document.getElementById("c" + x[1]).innerHTML = "&nbsp;&nbsp;c, d, e&nbsp;&nbsp;"; document.getElementById("c" + x[2]).innerHTML = "&nbsp;&nbsp;f, g, h&nbsp;&nbsp;"; document.getElementById("c" + x[3]).innerHTML = "&nbsp;&nbsp;a, a, a&nbsp;&nbsp;"; function checkans(ans) { if (ans == "&nbsp;&nbsp;a, a, a&nbsp;&nbsp;") { alert(ans); } }代码有效:

<div>Click the "a, a, a" option to see an alert box</div>
<div class="ans" id="c1" onclick="checkans(this.innerHTML)"></div>
<div class="ans" id="c2" onclick="checkans(this.innerHTML)"></div>
<div class="ans" id="c3" onclick="checkans(this.innerHTML)"></div>
<div class="ans" id="c4" onclick="checkans(this.innerHTML)"></div>
WITH del AS (
        DELETE FROM a
        WHERE EXISTS ( SELECT *
                FROM b
                WHERE b.id = a.id
                )
        returning *
        )
INSERT INTO a (id, x, y, z)
SELECT id, x, y, z
FROM b
WHERE EXISTS (
        SELECT *
        FROM del
        WHERE del.id = b.id
        );