javascript 根据 if 语句显示隐藏的 div 部分
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7239867/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
show hidden div section based on if statement
提问by ABI
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.
谢谢。
采纳答案by Mihai Iorga
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 是基于服务器端的,代表预处理超文本,而 Javascript 是基于浏览器的。
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 之后。
回答by J0HN
<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 withinthe <script>
需要注意的是PHP代码中的<script>
回答by Dawson
Why don't you write your function to accept the id
param of the query string?
<script>thefunction($get);</script>
为什么不编写函数来接受id
查询字符串的参数?
<script>thefunction($get);</script>
This way you can save on if
logic, and develop a more abstract function. It's less code, and easier to manage.
通过这种方式,您可以节省if
逻辑,并开发更抽象的功能。它的代码更少,更易于管理。
回答by Oliver M Grech
First of all. I would use jquery to show/hide divs...
首先。我会使用 jquery 来显示/隐藏 div...
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 :)
希望你明白我的想法,它干净而且非常有活力:)
回答by Marcel
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';
}