在 php 中隐藏/显示 url 字符串中的 div
一个完整的初学者问题。 我在页面(page2.php)上有大量的div(> 80),我想做的是打开page1.php,单击链接打开page2.php并仅显示其中一个div,具体取决于单击了哪个链接。
我通过向 div 添加 if else 来获得此功能的基本工作版本。到目前为止,我只在 5 个 div 上执行过此操作,并且它有效,但它似乎也是一种相当不雄辩的做事方式。
第 1 页:
<a href="page2.php?id=r0101">this is a link</a>
第 2 页:
<?php
$divID = $_GET['id'];
?>
<div id="r0101" <? if($divID == r0101): ?>class="show"<? else: ?>class="hidden"<? endif; ?> >
然后应用 css 类来隐藏或显示 div。 是否可以在页面顶部有一个函数或其他任何东西,从 url 中获取 id,找出也有一个具有该 id 的 div,显示它并隐藏所有其他内容?这可能是一件很容易做到的事情,但它让我难住了。
非常感谢任何帮助。 谢谢。
A complete beginners question.
I have a large number of divs (>80) on a page (page2.php) and what I would like to do is open page1.php, click on a link to open page2.php and show only one of these divs depending on which link was clicked.
I have a basic working version of this by adding an if else to the divs. I've only done this on 5 of the divs so far and it works but it also seems a fairly in-eloquent way of doing things.
Page 1:
<a href="page2.php?id=r0101">this is a link</a>
Page 2:
<?php
$divID = $_GET['id'];
?>
<div id="r0101" <? if($divID == r0101): ?>class="show"<? else: ?>class="hidden"<? endif; ?> >
This then applies a css class to hide or show the div.
Would it be possible to have a function or whatever at the top of the page that takes the id from the url, figures out that there is a div with that id also, show it and hide all the others? This is probably an easy thing to do but it has me stumped.
Any help greatly appreciated.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
更不用说 div 和 css 了(当你依靠它来隐藏/显示 div 时)。
您不仅可以生成标记,还可以生成 css 样式表。使用类似的(将其放在
头部部分的末尾)。让浏览器为您完成工作;)
Let alone the divs and work on css (as you relay on that to hide/show the divs).
You can generate not only markup but css stylesheet too. Use a similar one (put it at
the end of your head section). And let the browser do the work for you ;)
假设我已经正确阅读了问题,您有一组 div(r0101、r0102 等),并且希望根据您所在的页面仅显示其中一个。上面的代码创建了这些 div 的数组,循环并创建了 div。如果 div 与页面 url 中的 div 匹配,则该 div 的类为“show”,否则为“hidden”。
Assuming I have read the question correctly, you have a set of divs (r0101, r0102, etc.) and wish to show only one of these depending on the page you are on. The code above creates an array of these divs, loops through and creates the div. The class of the div is 'show' if the div matches the div from the page url, and 'hidden' otherwise.
首先,您应该考虑一种使 div 动态打印的方法。类似的东西:
此外,如果您找到了一种执行上述操作的方法,您还可以执行类似的操作:
或者
完全相同,但是(使用 三元运算符)节省了几行,(有些人认为)它降低了可读性。
First of all, you should consider a way of making your divs to be printed dynamically. Something like:
Also, if you find a way of doing what's stated above, you can also do something like:
or
which is exactly the same but (using the ternary operator) spares a few lines and (some people think) it decreases readability.
如果你想让你的下载更快,你应该只输出你想要显示的div。你可以做这样的事情:
希望这有帮助。
If you want to make your download faster, you should output only the div you want to show. You could do something like this:
Hope this helps.