我正在寫一個wordpress插件注入一些JavaScript和HTML到一個職位。使用「the_content」過濾器我設法插入我的標籤與我所有的JavaScript函數和所有我的HTML標記之後。問題是腳本不工作,螢火蟲標籤它是一個語法錯誤,而不是非常精確的錯誤在哪裏。使用WordPress的the_content過濾器注入JavaScript產生JavaScript語法錯誤
下面是插入頁面的javascript標記的內容,當我檢查它時,它似乎沒有任何類型的語法錯誤。
<![CDATA[
Votes = {};
$(document).ready(function()
{
$(".starContainer span").hover(sr_addBG("http://revealcoupons.com/wp-content/plugins/StarRatingLite/images/stars.png"), sr_removeBG());
});
function sr_addBG(ImgURL){
$(this).css("background","transparent url("+ImgURL+") repeat-x 0 -60px");
}
function sr_removeBG(){
$(this).css("background","none");
}
function sr_castVote(ID,Vote, ImgURL){
Votes[ID] = Vote;
$("#starContainer"+ID+" span").css("background","none");
$("ID"+ID+"Star"+Vote).css("background","transparent url("+ImgURL+") repeat-x 0 -60px");
}
function sr_verifyVote(NbFeatures){
var count = 0;
for (var e in Votes)
{count++;}
return count >= NbFeatures;
}
function sr_submitVotes(NbFeatures, PostID){
if (sr_verifyVote(NbFeatures))
{
ajaxReq = new XMLHttpRequest();ajaxReq.onreadystatechange=function()
{
if (ajaxReq.readyState == 4 && ajaxReq.status == 200)
{
alert("BAM! vote submitted. Thank you");
}
}
var voteStr = "";
for (var vote in Votes)
{voteStr += vote+"[eq]"+Votes[vote]+"[amp]";}
voteStr = voteStr.substring(0, voteStr.length - 5);
ajaxReq.open("http://revealcoupons.com/wp-content/plugins/StarRatingLite/StarRatingLite.php?PostID="+PostID+"&votes="+voteStr");
ajaxReq.send();
}
else
{
alert("You must vote on all features before submitting your opinion.");
}
}]]>
螢火蟲產生的唯一錯誤是「語法錯誤」,而不是更具體。我唯一沒有寫的東西就是cdata部分的結尾,其中「>」字符被「>」自動替換。
感謝您的任何意見!
可以粘貼Firebug的錯誤輸出? – Einacio
螢火蟲輸出實際上是整個JavaScript沒有換行符和標籤,因爲我用preg_replace刪除它們。我重新格式化了它的可讀性 – Louis