&& 破坏了 WordPress 中的页面

问题描述 投票:0回答:4

我将此添加到我的 WordPress 页面

if (script.readyState && script.onload!==null){
    script.onreadystatechange= function () {
        if (this.readyState == 'complete') mce_preload_check();
    }
}

并且

&&
正在转向

if (script.readyState && script.onload!==null){

我将其粘贴到 WordPress HTML 视图中,并确保这没问题,但 WordPress 一直显示此内容。如何解决这个问题?

javascript html wordpress
4个回答
9
投票

您需要禁用 WP 的自动格式化。即使在 html 编辑器中,WP 也会自动格式化,并且空格和换行符会破坏您的 javascript。

使用此插件http://wordpress.org/extend/plugins/wp-no-format/

更新 4/08/2015:插件已过时,但仍然适用于我。

这也有效:将插件直接添加到functions.php并将您的javascript括在

<!-- noformat on -->
<!-- noformat off -->
标签中

添加到functions.php文件:

function newautop($text)
{
    $newtext = "";
    $pos = 0;

    $tags = array('<!-- noformat on -->', '<!-- noformat off -->');
    $status = 0;

    while (!(($newpos = strpos($text, $tags[$status], $pos)) === FALSE))
    {
        $sub = substr($text, $pos, $newpos-$pos);

        if ($status)
            $newtext .= $sub;
        else
            $newtext .= convert_chars(wptexturize(wpautop($sub)));      //Apply both functions (faster)

        $pos = $newpos+strlen($tags[$status]);

        $status = $status?0:1;
    }

    $sub = substr($text, $pos, strlen($text)-$pos);

    if ($status)
        $newtext .= $sub;
    else
        $newtext .= convert_chars(wptexturize(wpautop($sub)));      //Apply both functions (faster)

    //To remove the tags
    $newtext = str_replace($tags[0], "", $newtext);
    $newtext = str_replace($tags[1], "", $newtext);

    return $newtext;
}

function newtexturize($text)
{
    return $text;   
}

function new_convert_chars($text)
{
    return $text;   
}

remove_filter('the_content', 'wpautop');
add_filter('the_content', 'newautop');

remove_filter('the_content', 'wptexturize');
add_filter('the_content', 'newtexturize');

remove_filter('the_content', 'convert_chars');
add_filter('the_content', 'new_convert_chars');

0
投票

另一种选择是制作短代码。在此示例中,仅当短代码包含属性

x
y
时才会打印,例如:
[myscript x="10" y="20"]
。我正在使用一个简单的脚本,该脚本显示带有属性值的 JS 警报对话框。

add_shortcode( 'myscript', 'sample_shortcode_so_6195635' );

function sample_shortcode_so_6195635( $atts, $content = null )
{   
    if( isset( $atts['x'] ) && isset( $atts['y'] ) )
    {
        $x = $atts['x'];
        $y = $atts['y'];

        // See: http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc
        $html = <<<HTML
        <button onclick="myalert()">Show Shortcode Atts</button>

        <script type="text/javascript">
        function myalert()
        {
            if( $x < 10 && $y < 20 )
                alert( 'X less than 10 and Y less than 20' );
            else
                alert( 'other' );
        }
        </script>   
HTML;
        return $html;
    }
}

0
投票

确保以下行(通过内联 PHP(您需要一个插件)或在“functions.php”中)在您的页面上执行:

remove_filter( 'the_content', 'wpautop' );
remove_filter( 'the_content', 'wptexturize' );
remove_filter( 'the_content', 'convert_chars' );

0
投票

将此代码添加到您的脚本中

<script>
  function logicalAND($opA, $opB) {
    if($opA) {
      if($opB) {
        return true;
      }
    }
    return false;
  }
  if (logicalAND(script.readyState, script.onload) !== null){
    ...
</script>
© www.soinside.com 2019 - 2024. All rights reserved.