我有
<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 中不工作。请帮忙!!
最佳答案
我不认为你可以使用 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
关于javascript - 用无序列表替换下拉列表。 JavaScript 方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4475939/