Javascript 如何更改动态创建的下拉列表的宽度?
我已经动态创建了下拉列表,试图设置其大小,但失败了Javascript 如何更改动态创建的下拉列表的宽度?,javascript,jquery,css,drop-down-menu,Javascript,Jquery,Css,Drop Down Menu,我已经动态创建了下拉列表,试图设置其大小,但失败了 code var L = document.createElement('label'); L.id = 'L_range'; var S = document.createElement('select'); S.id = 'range'; var op1 = document.createElement('option'); var op2 = document.createElement('opti
code
var L = document.createElement('label');
L.id = 'L_range';
var S = document.createElement('select');
S.id = 'range';
var op1 = document.createElement('option');
var op2 = document.createElement('option');
var op3 = document.createElement('option');
var op4 = document.createElement('option');
op1.appendChild(document.createTextNode('10'));
op1.id = '10';
op2.appendChild(document.createTextNode('25'));
op2.id = '25';
op3.appendChild(document.createTextNode('50'));
op3.id = '50';
op4.appendChild(document.createTextNode('100'));
op4.id = '100';
S.appendChild(op1);
S.appendChild(op2);
S.appendChild(op3);
S.appendChild(op4);
L.appendChild(S);
L.appendChild(document.createTextNode('records per page'));
var root = document.getElementById('sel');
root.innerHTML = '';
root.appendChild(L);
我试过
S.size='1'
但是没有区别。size属性不处理select的宽度。()
只要通过css给它一个宽度,你就可以走了
jquery
var values=[0,1,2,3,500,700,100,565,475,52556,5225,1111];
$.each(values, function(k,v){
$('#myselect').append($('<option>').val(v).html(v));
});
$('#myselect').css('width','200px');
var值=[0,1,2,350070001005654755255652251111];
$。每个(值、函数(k、v){
$('#myselect').append($(''.val(v).html(v));
});
$('#myselect').css('width','200px');
html
ps:别忘了把这段代码放在一个文档就绪函数中,否则它不会被调用
触发。(这在JSFIDLE中不需要)
var值=[0,1,2,350070001005654755255652251111];
$(函数(){
$。每个(值、函数(k、v){
$('#myselect').append($(''.val(v).html(v));
});
$('#myselect').css('width','200px');
})
大小属性应该做什么?下拉列表的默认大小是宽的,要减小它,因为它只包含数字@所以你想调整输入的宽度,而不是高度?真是一团糟(很抱歉)。为什么不使用循环来填充selectbox?此代码可以减少到+-4行。您可以尝试使用类似S.setAttribute('style','width:100px')的内容设置宽度;
S.setAttribute('style', 'width:10px');
var values=[0,1,2,3,500,700,100,565,475,52556,5225,1111];
$.each(values, function(k,v){
$('#myselect').append($('<option>').val(v).html(v));
});
$('#myselect').css('width','200px');
<select id="myselect"></select>
var values=[0,1,2,3,500,700,100,565,475,52556,5225,1111];
$(function(){
$.each(values, function(k,v){
$('#myselect').append($('<option>').val(v).html(v));
});
$('#myselect').css('width','200px');
})