简体   繁体   English

PHP中的Javascript不起作用

[英]Javascript in PHP isn't working

I am using the supersized jquery script to use various sliding backgrounds for my wordpress page. 我正在使用超大型jquery脚本为我的wordpress页面使用各种滑动背景。

Now I wanted to make different slides for each site and need a php if request. 现在我想为每个站点制作不同的幻灯片,并且如果需要的话需要一个php。

My code: 我的代码:

<?php if ( is_page(array('Restaurant'))) { 
    echo"<script type="text/javascript">
        jQuery(function($) {

            $.supersized({

                // Functionality
                slide_interval: 9000, // Length between transitions
                transition: 1, // 0-None, 1-Fade, 2-Slide Top, 3-Slide Right, 4-Slide Bottom, 5-Slide Left, 6-Carousel Right, 7-Carousel Left
                transition_speed: 1400, // Speed of transition

                // Components                           
                slide_links: 'blank', // Individual links for each slide (Options: false, 'num', 'name', 'blank')
                slides: [ // Slideshow Images
                    {
                        image: 'http://www.hotel-zur-traube.eu/wp-content/themes/hotelzurtraube/images/bg.jpg',
                        title: 'Hotel-Pension-Restaurant Zur Traube in Altenahr'
                    }, {
                        image: 'http://www.hotel-zur-traube.eu/wp-content/themes/hotelzurtraube/images/bg2.jpg',
                        title: 'Hotel-Pension-Restaurant Zur Traube in Altenahr'
                    }, {
                        image: 'http://www.hotel-zur-traube.eu/wp-content/themes/hotelzurtraube/images/bg3.jpg',
                        title: 'Hotel-Pension-Restaurant Zur Traube in Altenahr'
                    },
                ]
            });
        });
    </script>";}
?>

But after saving the code in my .php file the site doesn't load anymore. 但是将代码保存到我的.php文件后,该站点不再加载。 If I remove the php if request everything works fine again. 如果我删除了php(如果要求),一切都会再次正常。

First I can suggest you to remove the echo of the code. 首先,我建议您删除代码的回显。

    <?php if ( is_page(array('Restaurant'))) { ?>
<script type="text/javascript">

        jQuery(function($){

            $.supersized({

                // Functionality
                slide_interval          :   9000,       // Length between transitions
                transition              :   1,          // 0-None, 1-Fade, 2-Slide Top, 3-Slide Right, 4-Slide Bottom, 5-Slide Left, 6-Carousel Right, 7-Carousel Left
                transition_speed        :   1400,       // Speed of transition

                // Components                           
                slide_links             :   'blank',    // Individual links for each slide (Options: false, 'num', 'name', 'blank')
                slides                  :   [           // Slideshow Images
                                                    {image : 'http://www.hotel-zur-traube.eu/wp-content/themes/hotelzurtraube/images/bg.jpg', title : 'Hotel-Pension-Restaurant Zur Traube in Altenahr'}, 
                                                    {image : 'http://www.hotel-zur-traube.eu/wp-content/themes/hotelzurtraube/images/bg2.jpg', title : 'Hotel-Pension-Restaurant Zur Traube in Altenahr'},  
                                                    {image : 'http://www.hotel-zur-traube.eu/wp-content/themes/hotelzurtraube/images/bg3.jpg', title : 'Hotel-Pension-Restaurant Zur Traube in Altenahr'},
                                            ]
            });
        });

    </script>
<?php } ?>

You'll have a best visibility of the code and will not have quotes errors. 您将对代码有最好的了解,并且不会出现引号错误。 Check your console with this method and tell us if you have an error 使用此方法检查控制台,并告诉我们是否有错误

Your code is not working because you have not escaped your strings. 您的代码无法正常工作,因为您没有对字符串进行转义。

When you use double-quotes as your opening and closing quotes then you must escape other double-quotes inside them. 当使用双引号作为开始和结束引号时,则必须在其中使用其他双引号。

This applies also for single-quotes meaning you must escape single-quotes that are inside strings that use single-quotes as the opening and closing quotes. 这也适用于单引号,这意味着您必须在使用单引号作为开始和结束引号的字符串内转义单引号。

NOTE: You do not need to escape double-quotes that are inside single-quotes and single-quotes that are inside double-quotes. 注意:您不需要转义单引号内的双引号和双引号内的单引号。

You need your escape your quotes like this: 您需要这样的报价转义:

<?php
  if(is_page(array('Restaurant'))) {
    echo("<script type=\"text/javascript\">
      jQuery(function($) {
        $.supersized({
          // Functionality
          slide_interval: 9000,
          // Length between transitions
          transition: 1,
          // 0-None, 1-Fade, 2-Slide Top, 3-Slide Right, 4-Slide Bottom, 5-Slide Left, 6-Carousel Right, 7-Carousel Left
          transition_speed: 1400,
          // Speed of transition
          // Components                           
          slide_links: 'blank',
          // Individual links for each slide (Options: false, 'num', 'name', 'blank')
          slides: [
            // Slideshow Images
            {
              image: 'http://www.hotel-zur-traube.eu/wp-content/themes/hotelzurtraube/images/bg.jpg',
              title: 'Hotel-Pension-Restaurant Zur Traube in Altenahr'
            }, {
              image: 'http://www.hotel-zur-traube.eu/wp-content/themes/hotelzurtraube/images/bg2.jpg',
              title: 'Hotel-Pension-Restaurant Zur Traube in Altenahr'
            }, {
              image: 'http://www.hotel-zur-traube.eu/wp-content/themes/hotelzurtraube/images/bg3.jpg',
              title: 'Hotel-Pension-Restaurant Zur Traube in Altenahr'
            }
          ]
        });
      });
    </script>");
 } ?>

You can also remove echo altogether like this: 您也可以像这样完全删除echo

<?php if(is_page(array('Restaurant'))) { ?>
  <script type="text/javascript">
    jQuery(function($) {
      $.supersized({
        // Functionality
        slide_interval: 9000,
        // Length between transitions
        transition: 1,
        // 0-None, 1-Fade, 2-Slide Top, 3-Slide Right, 4-Slide Bottom, 5-Slide Left, 6-Carousel Right, 7-Carousel Left
        transition_speed: 1400,
        // Speed of transition
        // Components                           
        slide_links: 'blank',
        // Individual links for each slide (Options: false, 'num', 'name', 'blank')
        slides: [
          // Slideshow Images
          {
            image: 'http://www.hotel-zur-traube.eu/wp-content/themes/hotelzurtraube/images/bg.jpg',
            title: 'Hotel-Pension-Restaurant Zur Traube in Altenahr'
          }, {
            image: 'http://www.hotel-zur-traube.eu/wp-content/themes/hotelzurtraube/images/bg2.jpg',
            title: 'Hotel-Pension-Restaurant Zur Traube in Altenahr'
          }, {
            image: 'http://www.hotel-zur-traube.eu/wp-content/themes/hotelzurtraube/images/bg3.jpg',
            title: 'Hotel-Pension-Restaurant Zur Traube in Altenahr'
          }
        ]
      });
    });
  </script>
<? } ?>

To make the code look a bit tidier you can remove the comments like this: 为了使代码看起来更整洁,您可以删除如下注释:

<?php if(is_page(array('Restaurant'))) { ?>
  <script type="text/javascript">
    jQuery(function($) {
      $.supersized({
        slide_interval: 9000,
        transition: 1,
        transition_speed: 1400,
        slide_links: 'blank',
        slides: [
          {
            image: 'http://www.hotel-zur-traube.eu/wp-content/themes/hotelzurtraube/images/bg.jpg',
            title: 'Hotel-Pension-Restaurant Zur Traube in Altenahr'
          }, {
            image: 'http://www.hotel-zur-traube.eu/wp-content/themes/hotelzurtraube/images/bg2.jpg',
            title: 'Hotel-Pension-Restaurant Zur Traube in Altenahr'
          }, {
            image: 'http://www.hotel-zur-traube.eu/wp-content/themes/hotelzurtraube/images/bg3.jpg',
            title: 'Hotel-Pension-Restaurant Zur Traube in Altenahr'
          }
        ]
      });
    });
  </script>
<? } ?>

<script type="text/javascript">更改为<script type='text/javascript'>您正在使用双引号-与打开和关闭echo语句相同

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM