&&正在破坏 WordPress 中的页面

发布于 2024-11-10 15:19:12 字数 518 浏览 0 评论 0原文

我将此添加到我的 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 一直显示此内容。如何解决这个问题?

I added this to my WordPress page

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

and the && is being turned to

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

I pasted this in WordPress HTML view and I made sure this was fine but WordPress keeps displaying this. How to address this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

冰之心 2024-11-17 15:19:12

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

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

更新 4/08/2015:插件已过时,但对我来说仍然有效。

这也有效:将插件直接添加到functions.php并将你的 JavaScript 在 标签

添加到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');

You need to disable WP's autoformatting. WP will auto format even in the html editor, and the spaces and line breaks will break your javascript.

Use this plugin http://wordpress.org/extend/plugins/wp-no-format/

Update 4/08/2015: plugin is dated but still works for me.

This also works: add the plugin directly to functions.php and bracket the your javascript in <!-- noformat on --> and <!-- noformat off --> tags

Add to functions.php file:

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');
﹏半生如梦愿梦如真 2024-11-17 15:19:12

另一种选择是制作短代码。在此示例中,只有包含属性 xy 的短代码才会被打印,例如:[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;
    }
}

Another option is to make a shortcode. In this example, the shortcode will only be printed if it contains the attributes x and y, e.g.: [myscript x="10" y="20"]. I'm using a simple script that shows a JS alert dialog with the attributes values.

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;
    }
}
友谊不毕业 2024-11-17 15:19:12

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

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

Make sure the following lines (either via inline PHP (you need a plugin for that) or in "functions.php") are being executed on your page:

remove_filter( 'the_content', 'wpautop' );
remove_filter( 'the_content', 'wptexturize' );
remove_filter( 'the_content', 'convert_chars' );
霊感 2024-11-17 15:19:12

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

<script>
  function logicalAND($opA, $opB) {
    if($opA) {
      if($opB) {
        return true;
      }
    }
    return false;
  }

  if (logicalAND(script.readyState, script.onload !== null)){
    ...
</script>

Add this code snippet to your script

<script>
  function logicalAND($opA, $opB) {
    if($opA) {
      if($opB) {
        return true;
      }
    }
    return false;
  }

  if (logicalAND(script.readyState, script.onload !== null)){
    ...
</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文