根据用户 ID 隐藏 div?

Hide div based on user ID?

我马上开始。我需要使用 PHP 和 Javascript.

在页面上隐藏一些内容

我正在使用 Joomla。这是 table:

<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 的用户隐藏 "Expiry Date" 和 "Batch 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>";
}

?>

现在,我的页面确实加载了,所以没有错误。但是,它仍然没有物理隐藏 div,其中 class 是 "testclass"。

它只是正常显示它们。我知道更好的方法是使用群组,我将为我们网站上的选定用户分配所需的群组。

所以我认为调用组可能比为所有用户 ID 调用更容易。

这是完成我正在使用的组件的自定义 "default.php" 的唯一事情。任何帮助是极大的赞赏。

FOA - 您的代码混合了 - 当 userID 为 50 时,代码显示 (.show()) class 元素,而不是隐藏它们。
第二 - 不要使用 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'> ... " 

注意引号。