从php添加属性到html元素if else条件

时间:2017-09-07 15:19:46

标签: javascript php jquery html codeigniter-3

我有2个数据库表。一个用于菜单名称,另一个用于子菜单名称。有些菜单没有儿童菜单。我想创建一个导航菜单,它将检查菜单是否有孩子。如果它有孩子,它将添加data-toggle =" dropdown"到该菜单,以便可以在下拉框中看到子菜单。这是我的代码,我不知道为什么我的代码不起作用或我的逻辑将如何在HTML中。



function myFunction()
{
  $(".dropdown-toggle").attr("data-toggle", "dropdown");
}
    

<nav class="navbar fixed-top navbar-expand-lg navbar-dark bg-dark fixed-top">
        <a class="navbar-brand" href="<?php echo base_url('welcome/index')?>">
            <img src="<?php echo base_url()?>assets/images/logos/logo.png" alt="LogoFOR2401" title="LogoFOR2401">
        </a>
        <button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
        </button>
        <div class="collapse navbar-collapse" id="navbarResponsive">
            <ul class="navbar-nav ml-auto">
                <?php foreach ($navigationmenu as $navigationmenu_item): ?>
                    <li class="nav-item">
                        <a href="<?php echo base_url('welcome/menu')?>/<?php echo $navigationmenu_item['MenuID']; ?>" class="nav-link dropdown-toggle" name="<?php echo $navigationmenu_item['MenuName']; ?>" id="navbarDropdownBlog" aria-haspopup="true" aria-expanded="false"><?php echo $navigationmenu_item['MenuName']; ?></a>                      
                        <div class="dropdown-menu dropdown-menu-right" aria-labelledby="navbarDropdownBlog">
                            <?php foreach ($submenu as $submenu_item): ?>
                                <?php if($navigationmenu_item['MenuID'] == $submenu_item['MenuID']) {?>        
                                    echo "<script type="text/javascript">myFunction();</script>";
                                    <!--echo "function myFunction();";-->
                                    <!--echo "</script>";-->
                                    <a class="dropdown-item" href="<?php echo base_url('welcome/submenu')?>/<?php echo $submenu_item['SubMenuID']; ?>"><?php echo $submenu_item['SubMenuName']; ?></a>
                                <?php } ?>  
                            <?php endforeach; ?>
                        </div>
                    </li>
                <?php endforeach; ?>
            </ul>
        </div>
    </nav>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:2)

有几个问题。

  1. 由于您使用?>退出脚本执行模式,因此不应使用echo。只需将HTML放在那里。
  2. 您的函数不会仅将该类添加到当前菜单,而是将该类添加到所有前面的元素class="dropdown-toggle"
  3. 每次循环使用id="navbarDropdownBlog"。 ID应该是唯一的。
  4. 你应该用PHP进行检查,而不是运行一个函数来添加类。

    <?php foreach ($navigationmenu as $navigationmenu_item):
        $submenus = array_filter($submenu, function($submenu_item) use ($navigationmenu_item) {
            return $submenu_item['MenuID'] == $navigationmenu_item['MenuID'];
        });
        $data = empty($submenus) ? '' : 'data-toggle="dropdown"';
        ?>
        <li class="nav-item">
            <a href="<?php echo base_url('welcome/menu')?>/<?php echo $navigationmenu_item['MenuID']; ?>" class="nav-link dropdown-toggle" name="<?php echo $navigationmenu_item['MenuName']; ?>" id="navbarDropdownBlog-<?php echo $navigationmenu_item['MenuID']; ?>" aria-haspopup="true" aria-expanded="false" <?php echo $data; ?>><?php echo $navigationmenu_item['MenuName']; ?></a>                      
            <div class="dropdown-menu dropdown-menu-right" aria-labelledby="navbarDropdownBlog-<?php echo $navigationmenu_item['MenuID']; ?>">
                <?php foreach ($submenus as $submenu_item): ?>
                    <a class="dropdown-item" href="<?php echo base_url('welcome/submenu')?>/<?php echo $submenu_item['SubMenuID']; ?>"><?php echo $submenu_item['SubMenuName']; ?></a>
                <?php endforeach; ?>
            </div>
        </li>
    <?php endforeach; ?>
    

    为了解决问题3,我将菜单ID附加到ID。