2011-11-03 68 views
0

我正在写一个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."); 
    } 
}]]&gt; 

萤火虫产生的唯一错误是“语法错误”,而不是更具体。我唯一没有写的东西就是cdata部分的结尾,其中“>”字符被“>”自动替换。

感谢您的任何意见!

+0

可以粘贴Firebug的错误输出? – Einacio

+0

萤火虫输出实际上是整个JavaScript没有换行符和标签,因为我用preg_replace删除它们。我重新格式化了它的可读性 – Louis

回答

0

您的.hover()是错误的。它期待2个功能处理,而是要传递undefined两次

尝试用这个,是我加的功能包装

$(".starContainer span").hover(function(){sr_addBG("http://revealcoupons.com/wp-content/plugins/StarRatingLite/images/stars.png")}, function(){sr_removeBG()}); 
+0

我已经添加了匿名函数包装器,但它仍然会产生相同的错误(语法错误)。有关此错误可能的来源的更多见解? – Louis

+0

您是否尝试删除CDATA部分的打开和关闭?或评论它 – Einacio