jQuery常用到的方法

一、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);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值