繁体   English   中英

PHP中的Javascript不起作用

[英]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(如果要求),一切都会再次正常。

首先,我建议您删除代码的回显。

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

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

暂无
暂无

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

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