Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/448.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/72.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_Jquery_Html_Wordpress - Fatal编程技术网

Javascript 将当前页面类转换为选择菜单';选定的';

Javascript 将当前页面类转换为选择菜单';选定的';,javascript,jquery,html,wordpress,Javascript,Jquery,Html,Wordpress,我目前有一个jQuery函数,它可以将wp_list_页面转换为一个选择菜单,当您选择页面时,该菜单会导航到所选页面。我想知道的是,我可以使用“current_page_item”类并将其转换为将当前页面设置为“selected”选项吗?我目前的代码如下: $('li.current_page_item').attr('selected', 'selected'); alert($('ul.selectdropdown')[0].selectedIndex ); $(function() {

我目前有一个jQuery函数,它可以将wp_list_页面转换为一个选择菜单,当您选择页面时,该菜单会导航到所选页面。我想知道的是,我可以使用“current_page_item”类并将其转换为将当前页面设置为“selected”选项吗?我目前的代码如下:

$('li.current_page_item').attr('selected', 'selected');
alert($('ul.selectdropdown')[0].selectedIndex );
$(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); 
            $select.change(function() { window.open($select.find(':selected').val(), '_top'); });
        });
        $(this).replaceWith($select);
    });
});
$('li.current\u page\u item').attr('selected','selected');
警报($('ul.selectdropdown')[0].selectedIndex);
$(函数(){
$('ul.selectdropdown')。每个(函数(){
变量$select=$('');
$(this).find('a').each(function(){
var$期权=$('');
$option.attr('value',$(this.attr('href')).html($(this.html());
$select.append($option);
$select.change(function(){window.open($select.find(':selected').val(),''u top');});
});
$(此).replaceWith($select);
});
});
试试这个:

$('li.current_page_item').attr('selected', 'selected');
alert($('ul.selectdropdown')[0].selectedIndex );
$(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());

            // Check for current page
            if(window.location.href == $(this).attr('href'))
            {
                $option.prop('selected',true).addClass('current_page_item');
            }

            $select.append($option); 
            $select.change(function() { window.open($select.find(':selected').val(), '_top'); });
        });
        $(this).replaceWith($select);
    });
});
$('li.current\u page\u item').attr('selected','selected');
警报($('ul.selectdropdown')[0].selectedIndex);
$(函数(){
$('ul.selectdropdown')。每个(函数(){
变量$select=$('');
$(this).find('a').each(function(){
var$期权=$('');
$option.attr('value',$(this.attr('href')).html($(this.html());
//检查当前页面
if(window.location.href==$(this.attr('href'))
{
$option.prop('selected',true.).addClass('current_page_item');
}
$select.append($option);
$select.change(function(){window.open($select.find(':selected').val(),''u top');});
});
$(此).replaceWith($select);
});
});

代码是否不起作用?如果没有,什么不起作用?你能给我们一个链接到JsFiddle.net上的演示吗?在第三行生成一个错误“$('ul.selectdropdown')[0]未定义”。这是你的代码员。我只添加了
window.location.href==$(this.attr('href'))
条件如果代码对您有效,您介意单击我答案旁边的复选标记吗?这样我就可以获得学分了?这就是我们在网站上获得积分的方式:)谢谢!