簡體   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