2009-12-13 26 views
15

¿Cómo convierto una lista desordenada en este formato¿Cómo convertir la lista desordenada en una lista desplegable de <select> con 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> 

en un desplegable en este formato

<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> 

usando jQuery?

Editar: Al seleccionar una entrada de la lista desplegable/seleccionar, el enlace debe abrirse en una nueva ventana o pestaña automáticamente. También quiero capaces de estilo que desea: http://www.dfc-e.com/metiers/multimedia/opensource/jqtransform/

+0

@Dominic Rodger - El código i dado es, por ejemplo, –

Respuesta

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

En respuesta a su último comentario, he modificado un poco, pero no lo he probado. Házmelo saber.

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

thx estoy intentando tu solución –

+0

no se está abriendo el enlace en una nueva ventana http://jsbin.com/etoyu –

+0

¿No está funcionando o no es lo que quieres? – czarchaic

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

EDITAR

Al igual que con cualquier código jQuery que desea ejecutar al cargar la página, hay que envolverlo en el interior $(document).ready(function() { ... }); bloque, o dentro de él de versión más corta $(function() { ... });. Actualicé la función para mostrar esto.

EDITAR

Hay fue un error en mi código también, trató de tomar href del elemento li.

+0

, pero que no está funcionando http://jsbin.com/omulu –

+0

aún no funciona http://jsbin.com/uvabi –

+0

Usted está tratando de ejecutar el script Dentro de las etiquetas