2010-12-18 82 views
1

我有用无序列表替换下拉列表。 JavaScript的方式

<div id="edit-country-wrapper"> 
    <select id="edit-country" class="form-select" name="country"> 
    <option selected="selected" value="all">All</option> 
    <option value="canada">Canada</option> 
    <option value="usa">USA</option> 
    </select> 
</div> 

并希望将其更改为

<div id="edit-country-wrapper"> 
    <ul id="edit-country" class="form-select" name="country"> 
    <li><a class="selected" href="/all">All</a></li> 
    <li><a class="" href="/canada">Canada</a></li> 
    <li><a class="" href="/usa">USA</a></li> 
    </ul> 
</div> 

这就是我现在所拥有的。

$(document).ready(function() { 
    $('#edit-country-wrapper select').each(function() { 
     var $select = $('<div id="location-select" />'); 
     $(this).find('option').each(function() { 
      var $option = $('<a />'); 
      $option.attr({ 
       href: '?country=' +$(this).attr('value'), 
       id: 'location-' + $(this).attr('value'), 
       class: $(this).attr('selected'), 
       html: $(this).html() 
      }); 
      $select.append($option); 
     }); 

     $(this).replaceWith($select); 
    }); 
}); 

我想用无序列表替换下拉列表。我已经在FF & Chrome中工作,但不在IE中。请帮忙!!

+0

什么东西不能在IE浏览器?收到任何错误?生成的HTML是否正常? – alex 2010-12-18 00:45:39

+0

IE不会将下拉列表转换为列表,但FF和Chrome会将其转换为列表。 – canintex 2010-12-20 19:44:11

回答

1

我不认为你可以使用attr来设置这样的HTML(我think它只会在html链接上创建一个新的属性)。

尝试更换与...

var $option = $('<a />', { 
       href: '?country=' +$(this).attr('value'), 
       id: 'location-' + $(this).attr('value'), 
       class: $(this).attr('selected'), 
       html: $(this).html() 
      }); 

只要你使用jQuery> = 1.4

+0

我正在使用jQuery 1.2.6 – canintex 2010-12-20 20:59:28

+0

对不起... jQuery 1.3.2 – canintex 2010-12-20 21:48:11

+0

@Erik在这种情况下,你不能像这样设置'html'。你想链接一个单独的方法'html()',它的第一个参数是HTML。 – alex 2010-12-20 23:07:19

1
$option.attr({ 
    href: '?country=' +$(this).attr('value'), 
    id: 'location-' + $(this).attr('value'), 
    class: $(this).attr('selected'), 
}).html($(this).html());