Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使活动项在加载时在页面的下拉列表中选中_Javascript_Laravel - Fatal编程技术网

Javascript 如何使活动项在加载时在页面的下拉列表中选中

Javascript 如何使活动项在加载时在页面的下拉列表中选中,javascript,laravel,Javascript,Laravel,将此UL转换为下拉列表 转换下拉列表 如何在页面的下拉列表中将活动项启用为selected=true。 **它总是在下拉列表的第一个项目中被选中可能类似这样的工作方式:在每个循环中 $('ul.showdiv').each(function() { var $select = $('<select />'); $(this).find('a').each(function() { var $option = $('<option />');

将此UL转换为下拉列表

转换下拉列表

如何在页面的下拉列表中将活动项启用为selected=true。
**它总是在下拉列表的第一个项目中被选中

可能类似这样的工作方式:在每个循环中

$('ul.showdiv').each(function() {
    var $select = $('<select />');
    $(this).find('a').each(function() {
        var $option = $('<option />');
        $option.attr('value', $(this).attr('href')).html($(this).html());
        if($(this).children().hasClass('active')){
            $option.prop('selected', true);
        }
        $select.append($option);
    });
    $(this).replaceWith($select);
});

像这样的怎么样?我知道它会稍微改变你的代码,但它是有效的。只需相应地更改文件名

应使用的文件名示例:userExperienceDesign.html,以确保导航菜单正常工作

从您自己的文件运行时,只需删除以下行:filename=item3.html

//用于选择活动链接的id的URL var href=window.location.href; var filename=href.split'/'.pop; filename=item3.html; //应使用的文件名示例:staffauggement.html,以确保导航菜单正常工作 filename=filename.replace/\..+$/; $nav_u2;+filename.children*.addClassactive; 如果$nav_u2;+filename.parent.parentli{ $nav_+filename.parent.parent.children*.addClassactive; } //固定子菜单宽度:最小宽度与父导航项相同,若子菜单项的右侧超过父ul的宽度,则左偏移补偿 var navWidth=$nav.width; var navLeft=$nav.offset.left; var navTotal=navWidth+navLeft; var子菜单=$nav.childrenli.childrenul; 对于var i=0;i<子菜单长度;i++{ var subMenu=subMenus.eqi; 子菜单.parent.childrenspan.css{padding right:30+px}; 子菜单.parent.addClasshasSubMenu; 子菜单.childrenli.css{ 最小宽度:子菜单.parent.width-5+px }; var subMenuWidth=subMenu.width; var submenulft=subMenu.offset.left; var submonutotal=子菜单宽度+子菜单左侧; 如果navTotal0{ $this.findul.toggle; } }; html,正文{ 填充:0px; 边际:0px; 背景颜色:灰色; } 导航{ 位置:绝对位置; 列表样式类型:无; 填充:0px; 左边距:5px; 边缘顶部:5px; 背景色:白色; 字体大小:20px; 空白:nowrap; } 导航*{ -webkit用户选择:无; /*Chrome all/Safari all*/ -moz用户选择:无; /*Firefox all*/ -ms用户选择:无; /*IE 10+*/ /*目前还不支持这些,使用风险自负*/ -o-用户选择:无; 用户选择:无; } 导航>李{ 显示:内联块; 位置:相对位置; 保证金:-5px; 填充:0px; } 导航>李。哈苏菜单:之后{ 内容:\25BE; 显示:内联块; 位置:绝对位置; 右:14px; 底部:13px; z指数:2; 字体大小:15px; } 导航李>*{ 显示:块; 颜色:黑色; 文字装饰:无; 填充:10px; 背景色:白色; 文本对齐:居中; 光标:指针; } 导航>李>*{ 右侧填充:15px; } 导航li>a:悬停,导航li>span:悬停{ 颜色:白色; 背景色:bbb; } 导航ul{ 显示:无; 位置:绝对位置; 列表样式类型:无; 填充:0px; 左边距:0px; 背景色:白色; 字号:18px; 空白:nowrap; } .主动{ 背景色:789abc!重要; 颜色:bbb; } 增加工作人员
如果$this.children.hasClass'active'{$option.prop'selected',true;}否则{}ul被转换为,并且活动类是很好的,我已经更新了children部分,但是您不必像在回答中那样重复代码。我的应该有用
Converting ul by this script.

     <script type="text/javascript">  $(function() {
         $('ul.showdiv').each(function() {
             var $select = $('<select />');
             $(this).find('a').each(function() {
                 var $option = $('<option />');
                 $option.attr('value', $(this).attr('href')).html($(this).html());
                 $select.append($option);
             });
             $(this).replaceWith($select);
         });

         $('select').on('change', function (e) {    
         var valueSelected = this.value;    
         window.location.href = valueSelected;  });  });
$('ul.showdiv').each(function() {
    var $select = $('<select />');
    $(this).find('a').each(function() {
        var $option = $('<option />');
        $option.attr('value', $(this).attr('href')).html($(this).html());
        if($(this).children().hasClass('active')){
            $option.prop('selected', true);
        }
        $select.append($option);
    });
    $(this).replaceWith($select);
});