javascript - 使用两个 var 正确调用函数

标签 javascript php jquery

我有 php 函数调用 2 个变量来控制事件链接。我不知道它写得是否正确以及如何在我的其他脚本中调用它。

我的函数位于functions.php中,我的导航位于nav.php中。我将两者都包含在index.php中。

功能:

function active_links() {

  // stranice = Active //
  if (isset($_GET['poslovni_korisnici'])) {
    $active1 = 'class="active"';
  } else {
    $active1 = '';
  }
  if (isset($_GET['korisnici'])) {
    $active2 = 'class="active"';
  } else {
    $active2 = '';
  }
}

导航.php

<body>
  <!-- start body -->

  <nav class="navbar navbar-default">
    <!-- start nav -->
    <div class="container-fluid">
      <!-- Brand and toggle get grouped for better mobile display -->
      <div class="navbar-header">
        <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
          <span class="sr-only">Toggle navigation</span>
          <span class="icon-bar"></span>
          <span class="icon-bar"></span>
          <span class="icon-bar"></span>
        </button>
        <a class="navbar-brand" href="index.php">POSLOVNA STATISTIKA</a>
      </div>
      <!-- Collect the nav links, forms, and other content for toggling -->
      <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
        <ul class="nav navbar-nav">
          <li ' . $active . '><a href="index.php?poslovni_korisnici">Poslovni korisnici <span class="sr-only">(current)</span></a>
          </li>
          <li ' . $active . '><a href="index.php?korisnici">Korisnici</a>
          </li>
        </ul>
      </div>
      <!-- /.navbar-collapse -->
    </div>
    <!-- /.container-fluid -->
  </nav>

index.php:

<?php

require_once 'functions.php';

$dbh = connect_to_database();

$active =  active_links();


include 'includes/head.php';

include 'includes/nav.php';

最佳答案

结束 active_links()

return(array($active1,$active2)); 

然后

$active =  active_links(); 

$active 将是一个包含 2 个要使用的变量的数组:

$active[0]$active[1]

关于javascript - 使用两个 var 正确调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28014546/

相关文章:

javascript - 在循环再次迭代之前,如何等待 Promise 的 "then" block 在嵌套的 forEach 循环中执行?

PHP MySQL 数据库测试

javascript - 如何触发绝对位置验证引擎功能来验证按钮单击时的字段?

php - mysql 列的总和

javascript - 如何删除模态中的属性选定选项

javascript - 使用 jQuery 定位动态 HTML

javascript - 如何在reactjs中成功登录后重定向并阻止未登录的页面?

javascript - 解析命题逻辑串

javascript - XHTML5 中的 SVG : setting attributes with proper namespace

PHP 表单输入不会给我文本字段.. [http-auth login for security]