1. 程式人生 > >JS錯誤 Uncaught SyntaxError Unexpected token ILLEGAL

JS錯誤 Uncaught SyntaxError Unexpected token ILLEGAL

col www. .com 大神 http ref 易懂 string 幽默

$(‘tbody‘, ‘#‘ + tableId).append(‘<tr onmouseover="this.style.backgroundColor=\‘#eeeeee\‘">xxx</tr>‘);

用js的onmouseover鼠標事件動態改變背景色

如果是this.style.backgroundColor=#eeeeee

將出現Uncaught SyntaxError: Unexpected token ILLEGAL錯誤 未捕獲的SyntaxError:意外的非法標記

如果改為this.style.backgroundColor="#eeeeee"或者this.style.backgroundColor=\"#eeeeee\"

將出現Uncaught SyntaxError: Unexpected token }錯誤 未捕獲的SyntaxError:意外的標記 }

因為 onmouseover="this.style.backgroundColor=#eeeeee" 外面已經有一對引號了

正確的做法應該是 onmouseover="this.style.backgroundColor=\‘#eeeeee\‘" ,總之就是引號標記錯誤

還有更繞的

var s = "\""+dataObj[$(th_obj).attr(‘SID‘)]+"\"";
dataObj = "<a href=‘#‘ onMouseOver=‘mouseOver(this,event,"
+s+");‘>.....</a>"; //s是變量

在用js,jquery動態添加數據的時候 尤其註意‘ ”有時還需要轉義。


再分享一下我老師大神的人工智能教程吧。零基礎!通俗易懂!風趣幽默!希望你也加入到我們人工智能的隊伍中來!https://www.cnblogs.com/captainbed

JS錯誤 Uncaught SyntaxError Unexpected token ILLEGAL