繁体   English   中英

在php include上设置javascript变量

[英]Set javascript variable on php include

我有一个HTML,其中包含几个将Get请求发送到Handler.php文件的链接,例如

<a href="Handler?Node=FirstGroup"><div class="MyClass">First Group</div></a>
<a href="Handler?Node=SecondGroup"><div class="MyClass">Second Group</div></a>

然后Handler.php使用include将请求发送回去:

if(isset($_GET['Node'])) {
    $Node=$_GET['Node'];
    if ($Node=="FirstGroup"){
        include 'Header.php';
        include '1MainTitle.php';
        include '2Selector.php';
        include '3FirstGroup.php';
        include 'xfoot.php';
    }
    elseif ($Node=="SecondGroup"){
        include 'Header.php';
        include '1MainTitle.php';
        include '2Selector.php';
        include '3SecondGroup.php';
        include 'xfoot.php';
    }
}

我的问题在于Header.php 尽管我在浏览器(带有F12)中看到了javascript,检查器仍显示警告TypeError: slides[(slideIndex - 1)] is undefined ,并且第一张幻灯片未显示(该脚本用于显示多张照片,一个接一个)。

我的Header.php文件是这样的:

<!DOCTYPE html>

<html>
<head>
    <title>The Title</title>
    <link href="../Files/EB.css" rel="stylesheet" type="text/css" />
    <link rel="icon" href="../Files/Images/favicon.ico" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">


<script>
var slideIndex = 1;
showSlides(slideIndex);

function plusSlides(n) {
  showSlides(slideIndex += n);
}

function currentSlide(n) {
  showSlides(slideIndex = n);
}

function showSlides(n) {
  var i;
  var slides = document.getElementsByClassName("mySlides");
  var dots = document.getElementsByClassName("dot");
  if (n > slides.length) {slideIndex = 1}    
  if (n < 1) {slideIndex = slides.length}
  for (i = 0; i < slides.length; i++) {
      slides[i].style.display = "none";  
  }
  for (i = 0; i < dots.length; i++) {
      dots[i].className = dots[i].className.replace(" active", "");
  }
  slides[slideIndex-1].style.display = "block";  
  dots[slideIndex-1].className += " active";
}
</script>

</head>

<body>

请注意, slideIndex在脚本中定义。

我的问题是:浏览器从php加载后会“读取” javascript包含?

在您的showSlides函数中,未在所有情况下都定义slideIndex

它被定义为n > slides.length

它也被定义为n < 1

否则,它是未定义的。

在上面的代码中,将n作为1传递给函数,因此,如果有多于1张幻灯片, slideIndex仍未定义。

暂无
暂无

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

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