【问题标题】:JQuery .before() add html block escapeJQuery .before() 添加 html 块转义
【发布时间】:2014-03-12 05:16:43
【问题描述】:

我只想使用 JQuery 的 .before() 功能添加一个 html 块,这里是块:

$buttonValue = '<div class="addthis_toolbox addthis_default_style" '.$str .'>
                 <a class="addthis_button_facebook_like" fb:like:layout="button_count"></a>
                 <a class="addthis_button_tweet"></a>
                 <a class="addthis_button_pinterest_pinit" pi:pinit:layout="horizontal"></a>
                 <a class="addthis_counter addthis_pill_style"></a>
               </div>';

这是我使用的脚本:

    $script  = "(function($){\n";
    $script  .= "$(document).ready(function(){\n";            
    $script  .= "$( \".prod-view\" ).before( \"$buttonValue\" );";
    $script .=      "});    \n";
    $script .=      "})(jQuery);" ;

我已经尝试过 php addlashes() 函数,但没有成功

【问题讨论】:

  • 尝试将 $buttonValue 中的值包装在 $() 中
  • 为什么不把你的 JavaScript 写成真正的 JavaScript 而不是 PHP 字符串?
  • 请告诉我您要存档的内容

标签: javascript php jquery html append


【解决方案1】:

试试这个,

$buttonValue = '<div class="addthis_toolbox addthis_default_style" '.$str .'><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_button_pinterest_pinit" pi:pinit:layout="horizontal"></a><a class="addthis_counter addthis_pill_style"></a></div>';

脚本

$script  = "(function($){\n";
    $script  .= "$(document).ready(function(){\n";            
    $script  .= "$( \".prod-view\" ).before( '$buttonValue' );";
    $script .=      "});    \n";
    $script .=      "})(jQuery);" ;

与您的代码的不同之处在于将 html 设为单行,否则会导致 js 错误。然后在.before()中使用分号

【讨论】:

  • 我们也可以用这段代码去掉换行符$string = str_replace(array("\n", "\r"), '', $code);
  • Ya..sure...无论如何我很高兴能帮助你
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多