【问题标题】:Using wordpress the_content filter to inject javascript yields javascript syntax error使用 wordpress the_content 过滤器注入 javascript 会产生 javascript 语法错误
【发布时间】:2011-12-21 06:20:50
【问题描述】:

我正在编写一个 wordpress 插件来将一些 javascript 和 html 注入到帖子中。使用“the_content”过滤器,我设法在我的所有 javascript 函数和我的所有 html 标记之后插入我的标签。问题是脚本不起作用,firebug 将其标记为语法错误,而没有非常精确地确定错误所在。

这是插入到页面中的 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 部分的结尾,其中 ">" 字符自动替换为 ">"。

感谢您的任何意见!

【问题讨论】:

  • 你能粘贴萤火虫错误输出吗?
  • firebug 输出实际上是整个 javascript,没有换行符和制表符,因为我使用 preg_replace 删除了它们。为了便于阅读,我重新格式化了它

标签: javascript wordpress filter


【解决方案1】:

您的.hover() 错误。它需要 2 个函数处理程序,而不是您传递 undefined 两次

试试这个,我是否添加了函数包装器

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

【讨论】:

  • 我添加了匿名函数包装器,但它仍然产生相同的错误(语法错误)。有关此错误的可能来源的更多见解吗?
  • 您是否尝试过删除 CDATA 部分的打开和关闭?或评论它
猜你喜欢
  • 2013-01-19
  • 2012-11-28
  • 1970-01-01
  • 1970-01-01
  • 2012-01-22
  • 1970-01-01
  • 2015-04-08
  • 1970-01-01
  • 2011-07-10
相关资源
最近更新 更多