使用jQuery从选择菜单中删除列表项 [英] Remove list items from select menu with jQuery

查看:161
本文介绍了使用jQuery从选择菜单中删除列表项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一小段脚本,允许用户调整窗口大小,将无序列表转换为选择菜单。

I have a tiny bit of script that allows the user to resize the window to convert an unordered list into a select menu.

但是,我想

DEMO: http://jsfiddle.net/k7272/

所以基本上使用上面的例子,'test'部分不会

So basically using the example above, the part 'test' wouldn't appear in the select menu, but would still appear in unordered list.

如何实现这一目标?

这是我的Javascript:

Here is my Javascript:

// DOM ready
     $(function() {

      // Create the dropdown base
      $("<select />").appendTo("nav");

      // Create default option "Go to..."
      $("<option />", {
         "selected": "selected",
         "value"   : "",
         "text"    : "Go to..."
      }).appendTo("nav select");

      // Populate dropdown with menu items
      $("nav a").each(function() {
       var el = $(this);
       $("<option />", {
           "value"   : el.attr("href"),
           "text"    : el.text()
       }).appendTo("nav select");
      });

       // To make dropdown actually work
       // To make more unobtrusive: http://css-tricks.com/4064-unobtrusive-page-changer/
      $("nav select").change(function() {
        window.location = $(this).find("option:selected").val();
      });

     });

非常感谢:-D

推荐答案

定位 nav a 会找到它的所有子项,甚至子项,因此您需要像这样直接定位

targeting nav a will find all its children and even children of children, so you need to directly target them like this

$(nav> ul> li> a)。each(...

http://jsfiddle.net/k7272/1/

这篇关于使用jQuery从选择菜单中删除列表项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆