为什么我收到这个JS错误?

时间:2011-05-11 01:10:21

标签: jquery html debugging syntax

这是错误:

Uncaught SyntaxError: Unexpected token ILLEGAL
d.d.extend.globalEvaljquery-1.5.1.min.js:16
d.ajaxSetup.converters.text scriptjquery-1.5.1.min.js:16
bQjquery-1.5.1.min.js:16
vjquery-1.5.1.min.js:16
d.support.ajax.d.ajaxTransport.send.c

这是jQuery:

$("div#notif_js").html(' ');

$("div#notification_box").html('<div class="notification">
  <p><img alt="Justin meltzer" class="feed_image_notif" src="/system/photos/46/tiny/Justin Meltzer.jpeg?1303109121" /> JMeltz 
   added a comment to <b>second</b> by <img alt="Justin meltzer" class="feed_image_notif" src="/system/photos/45/tiny/Justin Meltzer.jpeg?1303109101" /> Justin Meltzer</p>
  <a href="/videos/506"></a>
</div>
<div class="notification">
  <p><img alt="Justin meltzer" class="feed_image_notif" src="/system/photos/46/tiny/Justin Meltzer.jpeg?1303109121" /> JMeltz 
   added a comment to <b>second</b> by <img alt="Justin meltzer" class="feed_image_notif" src="/system/photos/45/tiny/Justin Meltzer.jpeg?1303109101" /> Justin Meltzer</p>
  <a href="/videos/506"></a>
</div>
<div class="notification">
  <p><img alt="Justin meltzer" class="feed_image_notif" src="/system/photos/46/tiny/Justin Meltzer.jpeg?1303109121" /> JMeltz 
   added a comment to <b>second</b> by <img alt="Justin meltzer" class="feed_image_notif" src="/system/photos/45/tiny/Justin Meltzer.jpeg?1303109101" /> Justin Meltzer</p>
  <a href="/videos/506"></a>
</div>
<div class="notification">
  <p><img alt="Justin meltzer" class="feed_image_notif" src="/system/photos/46/tiny/Justin Meltzer.jpeg?1303109121" /> JMeltz upvoted bhbu </p>
   <a href="/videos/505"></a>  
</div>      
<div class="notification">
  <p class= "all_notifications">All Notifications</p>
  <a href="/videos/notification_go"></a>
</div>');

2 个答案:

答案 0 :(得分:1)

Javascript不允许您在字符串中包含多行。您需要将每一行转换为\ n,如下所示:

$("div#notification_box").html('line1\nline2');

或在每行结尾之前加上'\',如下所示:

$("div#notification_box").html('line1\
line2');

第一种方法似乎更常用,但我认为第二种方法看起来更好。

另外,检查javascript错误的一种简单方法是通过javascript delinter运行它,例如http://www.javascriptlint.com/online_lint.phphttp://www.jslint.com/(尽管jslint不会像javascriptline那样报告错误)


答案 1 :(得分:0)

使用jQuery 1.5.2 +

对我有用

http://jsfiddle.net/XSAb2/

编辑:我已将字符串转换为单行字符串,因为我认为只是StackOverflow格式化强制了多行