一、jQuery获取并设置 CSS 类
1、添加类:addClass(),例如:
$('#id').addClass('newclass');
2、移除类:removeClass(),例如:
$('#id').removeClass('oldclass');
3、进行添加/移除类的切换操作:toggleClass(),例如:
$('#id').toggleClass('isactive');
备注:newclass、oldclass、isactive,均为css样式
4、设置指定的 CSS 单个属性
$("p").css("background-color","yellow");
5、设置指定的 CSS 多个属性
$("p").css({"background-color":"yellow","font-size":"200%"});
6、获取当前对象的属性值
$('#mydiv').css('margin-top');//获取当前id的margin-top值
二、jQuery 属性操作attr() 方法:设置或获取被选元素的属性值。
$("img").attr("width");//获取img宽度
$("img").attr({width:"50",height:"80"});//设置img高度、宽度
三、动态生成input导入框
$("#inputfile").empty();
//方法一:
const input1 = document.createElement('input');
$(input1).attr('type', 'text');
$(input1).attr('class', 'form-control');
$(input1).attr('id', 'importText');
$('#inputfile').append(input1);
const input = document.createElement('input');
$(input).attr('type', 'file');
$(input).attr('accept', 'application/vnd.ms-excel, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
$(input).attr('id', 'fileList');
$('#inputfile').append(input);
// 方法二:
$("#inputfile").append('<input type="text" class="form-control" id="importText"/>');
$("#inputfile").append('<input type="file" accept="application/vnd.ms-excel, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" id="fileList"/>');
console.log($("#inputfile"));
const watchid =setInterval(()=>{
if($("#fileList").length){
console.log($("#fileList"));
clearInterval(watchid);
$("#fileList").click();
$('#fileList').change(function () {
that.importSure();
});
}
},100);