简体   繁体   English

根据if语句显示隐藏的div部分

[英]show hidden div section based on if statement

I have a few hidden divs on page like this 我在页面上有一些隐藏的div像这样

<div class="form" id="scheduler" style="display: none;">

<div class="form" id="test" style="display: none;">

<div class="form" id="super" style="display: none;">

I would like a jscript which can be called to show these hidden divs based on criteria like this 我想要一个可以基于这样的条件调用jscript来显示这些隐藏的div的脚本

<?php 
if ($_GET['id'] == 'scheduler') {
   call jscript function (show div id:scheduler in this case)
}
else if ($_GET['id'] == 'test') {
   call jscript function (show div id:test in this case)
}
else if ($_GET['id'] == 'super') {
   call jscript function (show div id:super in this case)
}
?>

thanks. 谢谢。

you cannot call javascript function from PHP, PHP is server side based and stands for Preprocesing Hypertext while Javascript is browser based. 您不能从PHP调用javascript函数,PHP是基于服务器端的,代表Java的基于浏览器的Preprocesing Hypertext。

You could use: 您可以使用:

<?php 
    if ($_GET['id'] == 'scheduler') {
       $showdiv = 'scheduler';
    }
    else if ($_GET['id'] == 'test') {
       $showdiv = 'test';
    }
    else if ($_GET['id'] == 'super') {
       $showdiv = 'super';
    }
    echo "<script type=\"text/javascript\">document.getElementById('".$showdiv."').style.display = 'block';</script>";
?>

but that should be at bottom of the page, after those divs are loaded. 但这应该在加载这些div之后位于页面底部。

<script type='text/javascript'>
function showDivById(id){
    document.getElementById(id).visible = true;
}

<?php 
    $divs = array('scheduler','test','super');
    if (array_seach($_GET['id'], $divs)!==false)
        echo "showDivById({$_GET['id']})";
?>
</script>

Note that php code is within the <script> 请注意,PHP代码 <script>

Why don't you write your function to accept the id param of the query string? 为什么不编写函数以接受查询字符串的id参数? <script>thefunction($get);</script>

This way you can save on if logic, and develop a more abstract function. 这样,您可以节省if逻辑,并开发更抽象的功能。 It's less code, and easier to manage. 它更少的代码,更易于管理。

First of all. 首先。 I would use jquery to show/hide divs... 我会用jquery显示/隐藏divs ...

but my concept would be a bit different then above, 但我的概念与上面的有所不同,

<?php
  if($_GET['id']){
    echo '<script type="text/javascript">$(\'#'.$_GET['id'].'\').show();</script>';
  }
?>

Hope that you got my idea, it's clean and pretty dynamic :) 希望你有我的主意,它很干净而且很动态:)

Even though you've accepted an answer, another option would be to use only JavaScript with the aid of some JS to easily retrieve values from the query string. 即使您已经接受了答案,另一种选择是借助某些JS仅使用JavaScript来轻松地从查询字符串中检索值。

function $get(variable) {
  var query = window.location.search.substring(1);
  var vars = query.split('&');
  for (var i = 0; i < vars.length; i++) {
    var pair = vars[i].split('=');
    if (pair[0] == variable) {
      return pair[1];
    }
  }
}

var qs_id = $get('id');

if (['scheduler', 'test', 'super'].indexOf(qs_id) >= 0) {
  document.getElementById(qs_id).style.display = 'block';
}

Demo: jsfiddle.net/Marcel/L69xt/show 演示: jsfiddle.net/Marcel/L69xt/show

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

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