Javascript 在 PHP 中不起作用

Javascript in PHP isn't working

我正在使用超大 jquery 脚本为我的 wordpress 页面使用各种滑动背景。

现在我想为每个站点制作不同的幻灯片,如果需要,需要 php。

我的代码:

<?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>";}
?>

但是在我的 .php 文件中保存代码后,该站点不再加载。如果我删除 php if request 一切正常。

<script type="text/javascript"> 更改为 <script type='text/javascript'> 您正在使用双引号 - 与打开和关闭 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>
<?php } ?>

您将对代码有最好的可见性,并且不会有引号错误。使用此方法检查您的控制台并告诉我们您是否有错误

您的代码无法正常工作,因为您没有对字符串进行转义。

当您使用双引号作为开始和结束引号时,您必须转义其中的其他双引号。

这也适用于单引号,这意味着您必须对使用单引号作为开始和结束引号的字符串内的单引号进行转义。

注意:您不需要转义单引号内的双引号和双引号内的单引号。

您需要像这样转义引号:

<?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>");
 } ?>

您也可以像这样一起删除 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>
<? } ?>

为了使代码看起来更整洁,您可以像这样删除注释:

<?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>
<? } ?>