未捕获的SyntaxError:意外的标记ILLEGAL

时间:2014-02-05 18:19:44

标签: javascript

我收到了Uncaught SyntaxError:chrome中的意外标记ILLEGAL,代码如下:

   var leftMiddleJS = 'function onmouseover() {
        document.getElementById("background").style.marginTop = "-135px"; 
        document.getElementById("background").style.marginLeft = "0px";
        document.getElementById("background").style.width = "760px";
        document.getElementById("background").style.height = "520px";
        document.getElementById("background").style.marginTop = "0";
        document.getElementById("background").style.marginLeft = "0";

        function click() { 
        document.getElementById("outsideContainer").style.marginTop = "0"; 
        document.getElementById("outsideContainer").style.marginLeft = "0"; 
        document.getElementById("outsideContainer").style.width = "300px"; 
        document.getElementById("outsideContainer").style.height = "250px"; 
        document.getElementById("insideContainer").style.marginTop = "-135px"; 
        document.getElementById("insideContainer").style.marginLeft = "0px"; }';

代码有什么问题?

1 个答案:

答案 0 :(得分:1)

如果要分配跨越多行的字符串 - 请使用\ string continuation:

var leftMiddleJS = 'function onmouseover() { \
        document.getElementById("background").style.marginTop = "-135px";  \
        document.getElementById("background").style.marginLeft = "0px"; \
        document.getElementById("background").style.width = "760px"; \
        document.getElementById("background").style.height = "520px"; \
        document.getElementById("background").style.marginTop = "0"; \
        document.getElementById("background").style.marginLeft = "0"; \
\
        function click() { \
        document.getElementById("outsideContainer").style.marginTop = "0"; \
        document.getElementById("outsideContainer").style.marginLeft = "0"; \
        document.getElementById("outsideContainer").style.width = "300px"; \
        document.getElementById("outsideContainer").style.height = "250px"; \
        document.getElementById("insideContainer").style.marginTop = "-135px";  \
        document.getElementById("insideContainer").style.marginLeft = "0px"; }';