使用.append()函数的Javascript和jQuery问题

时间:2011-07-31 10:47:12

标签: javascript jquery grails-plugin

我在使用Grails .gsp中的jQuery附加.append函数的div时遇到了麻烦。问题是我试图追加的div是一个RichUI星级评级块,当我使用标签时以编程方式编写。代码如下:

Grails中的代码.gsp:

var textToInsert = [];
textToInsert[1] = '<tr><td>';
textToInsert[2] = artistData.name;
textToInsert[3] = '</td><td><div id="artist2"><richui:rating dynamic="true" id="2" units="5" rating="0"  controller="rating" action="rate"/></div></td></tr>';

$j('#recommendedList').append(textToInsert.join(''));

生成的html就是这样:

var textToInsert = [];
                textToInsert[1] = '<tr><td>';
                textToInsert[2] = artistData.name;
                textToInsert[3] = '</td><td><div id="artist2"><div id='r3a44146338e7d18384c9233cb7f7bb82'>
<div class='ratingblock'>  
 <div id='unit_longr3a44146338e7d18384c9233cb7f7bb82'>
  <ul id='unit_ulr3a44146338e7d18384c9233cb7f7bb82' class='unit-rating' style='width: 150px'>
    <li id='r9cd7abbd875dc72e67c84180c5b41704' class='current-rating' style='width: 0px;'>Currently 0.0</li>
    <li><a href="/Ontourtest/rating/rate/2?rating=1" onclick="new Ajax.Updater('r3a44146338e7d18384c9233cb7f7bb82','/Ontourtest/rating/rate/2',{asynchronous:true,evalScripts:true,parameters:'rating=1'});return false;" class="r1-unit rater" title="1">1</a></li>
    <li><a href="/Ontourtest/rating/rate/2?rating=2" onclick="new Ajax.Updater('r3a44146338e7d18384c9233cb7f7bb82','/Ontourtest/rating/rate/2',{asynchronous:true,evalScripts:true,parameters:'rating=2'});return false;" class="r2-unit rater" title="2">2</a></li>
    <li><a href="/Ontourtest/rating/rate/2?rating=3" onclick="new Ajax.Updater('r3a44146338e7d18384c9233cb7f7bb82','/Ontourtest/rating/rate/2',{asynchronous:true,evalScripts:true,parameters:'rating=3'});return false;" class="r3-unit rater" title="3">3</a></li>
    <li><a href="/Ontourtest/rating/rate/2?rating=4" onclick="new Ajax.Updater('r3a44146338e7d18384c9233cb7f7bb82','/Ontourtest/rating/rate/2',{asynchronous:true,evalScripts:true,parameters:'rating=4'});return false;" class="r4-unit rater" title="4">4</a></li>
    <li><a href="/Ontourtest/rating/rate/2?rating=5" onclick="new Ajax.Updater('r3a44146338e7d18384c9233cb7f7bb82','/Ontourtest/rating/rate/2',{asynchronous:true,evalScripts:true,parameters:'rating=5'});return false;" class="r5-unit rater" title="5">5</a></li>
   </ul>
  </div>
 </div>
</div></div></td></tr>';

$j('#recommendedList').append(textToInsert.join(''));

Firebug给出错误:丢失;在声明textToInsert[3] = '</td><td><div ...='r3a44146338e7d18384c9233cb7f7bb82'>

之前

我的猜测是,当richui标签写入没有半冒号的新行字符时,会产生javascript错误,但我不确定。

有没有办法克服这个问题? 非常感谢!

1 个答案:

答案 0 :(得分:0)

这意味着此行存在错误(以及所有后续行,我不会全部复制):

textToInsert[3] = '</td><td><div id="artist2"><div id='r3a44146338e7d18384c9233cb7f7bb82'>

我认为错误是你没有逃避单引号。您可以使用双引号:

textToInsert[3] = '</td><td><div id="artist2"><div id="r3a44146338e7d18384c9233cb7f7bb82">'

如果你想保留单引号,你需要逃避它们:

textToInsert[3] = '</td><td><div id="artist2"><div id=\'r3a44146338e7d18384c9233cb7f7bb82\'>