我马上开始讲。我需要使用 PHP 和 Javascript 隐藏页面上的一些内容。
我正在使用 Joomla。这是表格:
<table class="orders">
<tr>
<th>Name</th>
<div class="testclass"><th>Expiry Date</th>
<th>Batch ID</th></div>
<th>Local ICOS</th>
</tr>
<tr>
<td>...</td>
<div class="testclass"><td>...</td>
<td>...</td></div>
<td>...</td>
</tr>
</table>
现在,假设我想隐藏用户 ID 为 50 的用户的“到期日期”和“批处理 ID”。这是我为此编写的代码:
<?php
$user = Jfactory::getUser();
$userID = $user->get(id);
if ($userID == "50") {
echo "<script type='text/javascript>'
$(document).ready(function(){
$('.testclass').show();
})
</script>";
}
else {
echo "<script type='text/'javascript>'
$(document).ready(function(){
$('.testclass').hide();
})
</script>";
}
?>
现在,我的页面已加载,因此没有错误。但是,它仍然没有物理隐藏类为“testclass”的 div。
它只是正常显示它们。我知道更好的方法是使用组,我将向我们网站上选定的用户分配所需的组。
所以我认为调用组可能比调用所有用户 ID 更容易。
这是完成我正在使用的组件的自定义“default.php”所剩下的唯一事情。任何帮助是极大的赞赏。
最佳答案
FOA - 您的代码将其混合 - 当 userID 为 50 时,代码显示 (.show()
) 类元素,而不是隐藏它们。
其次 - 不要使用 Javascript 隐藏内容,因为任何有权访问开发工具的人(因此任何人)都可以更改 css 并查看代码。
更好的方法是根本不渲染代码。它会像这样:
<table class="orders">
<tr>
<th>Name</th>
<?php if ($user->id!="50") { ?>
<th>Expiry Date</th>
<th>Batch ID</th>
</div>
<?php } ?>
<th>Local ICOS</th>
</tr>
</table>
当不重要的东西只是为了方便用户看不到它们时,你可以使用 JS 隐藏它们。不要对重要的事情这样做。
另外,您的 JS 代码在这两部分都不正确。它应该这样开始:
echo "<script type='text/javascript'> ... "
注意引号。
关于javascript - 根据用户ID隐藏div?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46383533/