如何使用jquery将无序列表转换为格式精美的<select>下拉列表?

Jit*_*yas 17 javascript css xhtml jquery

如何以此格式转换无序列表

<ul class="selectdropdown">
    <li><a href="one.html" target="_blank">one</a></li>
    <li><a href="two.html" target="_blank">two</a></li>
    <li><a href="three.html" target="_blank">three</a></li>
    <li><a href="four.html" target="_blank">four</a></li>
    <li><a href="five.html" target="_blank">five</a></li>
    <li><a href="six.html" target="_blank">six</a></li>
    <li><a href="seven.html" target="_blank">seven</a></li>
</ul>
Run Code Online (Sandbox Code Playgroud)

进入这种格式的下拉列表

<select>
    <option value="one.html" target="_blank">one</option>
    <option value="two.html" target="_blank">two</option>
    <option value="three.html" target="_blank">three</option>
    <option value="four.html" target="_blank">four</option>
    <option value="five.html" target="_blank">five</option>
    <option value="six.html" target="_blank">six</option>
    <option value="seven.html" target="_blank">seven</option>
</select>
Run Code Online (Sandbox Code Playgroud)

使用jQuery?

编辑:从选择/下拉列表中选择条目时,链接应自动在新窗口或选项卡中打开.我也希望能够设置它的样式:http://www.dfc-e.com/metiers/multimedia/opensource/jqtransform/

Tat*_*nen 21

$(function() {
    $('ul.selectdropdown').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);
    });
});
Run Code Online (Sandbox Code Playgroud)

编辑

与您希望在页面加载时运行的任何jQuery代码一样,您必须将其包装在$(document).ready(function() { ... });块内,或者在其较短版本内$(function() { ... });.我更新了函数来显示这个.

编辑

还有就是在我的代码也是一个错误,试图采取HREF从li元素.


cza*_*aic 14

$('ul.selectdropdown').each(function() {
    var select = $(document.createElement('select')).insertBefore($(this).hide());
    $('>li a', this).each(function() {
        var a = $(this).click(function() {
            if ($(this).attr('target')==='_blank') {
                window.open(this.href);
            }
            else {
                window.location.href = this.href;
            }
        }),
        option = $(document.createElement('option')).appendTo(select).val(this.href).html($(this).html()).click(function() {
            a.click();
        });
    });
});
Run Code Online (Sandbox Code Playgroud)

在回复你的上一条评论时,我对它进行了一些修改,但尚未对其进行测试.让我知道.

$('ul.selectdropdown').each(function() {
    var list = $(this), select = $(document.createElement('select')).insertBefore($(this).hide());

    $('>li a', this).each(function() {
        var target = $(this).attr('target'),
        option = $(document.createElement('option'))
            .appendTo(select)
            .val(this.href)
            .html($(this).html())
            .click(function(){
                if(target==='_blank') {
                    window.open($(this).val());
                }
                else {
                    window.location.href = $(this).val();
                }
            });
    });
    list.remove();
});
Run Code Online (Sandbox Code Playgroud)

  • 使用change(),但是在select元素上,而不是选项:`select.change(function(){window.location = $(this).find("option:selected").val();});` <-----这应该可以使上述代码在Chrome中运行. (2认同)