& lt; select>菜单导航与Wordpress


<select> menu navigation with Wordpress

我试图将Wordpress中的'列表页面'功能转换为动态选择菜单导航(如这里的第一个示例:http://lab.artlung.com/dropdown/)。我试过用js转换wp_list_pages,代码如下:

$(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);
});

});

这可以转换它,但不允许我插入所需的:

onchange="window.open(this.options[this.selectedIndex].value,'_top')"

我能把它放到上面的函数,或者有更好的方法去做这件事吗?

请帮忙就太好了。

<——edit——>下面的函数可以正常工作:

$("ul.selectdropdown").show();

$(函数(){$ (' ul.selectdropdown ') . each(函数(){Var $select = $(");

    $(this).find('a').each(function() {
        var $option = $('<option />');
        $option.attr('value', $(this).attr('href')).html($(this).html());
        $select.append($option); 
        $select.change(function() { window.open($select.find(':selected').val(), '_top'); });
    });
    $(this).replaceWith($select);
});

});

为什么不用$select.change(function() { window.open($select.find(':selected').val(), '_top'); });呢?