Javascript 将类指定给元素后';附加的

Javascript 将类指定给元素后';附加的,javascript,jquery,slider,setattribute,Javascript,Jquery,Slider,Setattribute,所以我现在有一个滑块,用户可以在上传图像时动态填充。我正在为滑块使用引导,这需要第一张幻灯片将类设置为“活动”才能工作,但是当我尝试使用将该类指定给第一张幻灯片时 "if ($("#carousel-inner").find('li')) { var slide1 = document.getElementById("slide-0"); slide1.setAttribute ("

所以我现在有一个滑块,用户可以在上传图像时动态填充。我正在为滑块使用引导,这需要第一张幻灯片将类设置为“活动”才能工作,但是当我尝试使用将该类指定给第一张幻灯片时


"if ($("#carousel-inner").find('li')) {

          var slide1 = document.getElementById("slide-0");

              slide1.setAttribute ("class", "active");

            }

它无法识别幻灯片id,并将null赋值,返回“cannot setAttribute of null”

有什么建议吗

HTML


JS


window.onload=函数(){
$('#上传图像')。单击(函数(){
$('#_imagesInput')。单击();
setTimeout(activeness(),5000)
});
$('.#_imagesInput')。在('change',function(){
handleFileSelect();
});
}
函数handleFileSelect(){
//检查文件API支持
if(window.File&&window.FileList&&window.FileReader){
var files=event.target.files;//文件列表对象
var输出=document.getElementById(“内部旋转木马”);
var arrfilescont=[];
var start=$(输出).find('li').length;
var end=start+files.length;
var nonImgCount=0;
var slide1=document.getElementById(“slide1”)
var display=document.getElementById(“displayImg”)
对于(变量i=开始;i<结束;i++){
arrfilescont.push(i);//推送到数组
}
如果(开始!==0){
$(输出).find('li>nav>a.prev').first().attr('href','#slide-'+(end-1));
$(输出).find('li>nav>a.next').last().attr('href','#slide-'+start);
}
对于(var i=0;i'++'';//TODO:输入标题
});
//阅读图片
picReader.readAsDataURL(文件);
}
if($(“#转盘内部”).find('li')){
var slide1=document.getElementById(“slide-0”);
slide1.setAttribute(“类”、“活动”);
}
}否则{
log(“您的浏览器不支持文件API”);
}
}
Hi,像这样试试:
$(“#旋转木马内部”).find('li:eq(0)).addClass(“active”)
看看这是否有效。Hi,像这样试试:
$(“#旋转木马内部”).find('li:eq(0)).addClass(“active”)
看看是否有效。
<div id="slideshow-container">
<div id="carouselExampleControls" class="carousel slide" data-ride="carousel">
  <ul class="carousel-inner" id ="carousel-inner">
 
</ul>
  <a class="carousel-control-prev" href="#carouselExampleControls" role="button" data-slide="prev">
    <span class="carousel-control-prev-icon" aria-hidden="true"></span>
    <span class="sr-only">Previous</span>
  </a>
  <a class="carousel-control-next" href="#carouselExampleControls" role="button" data-slide="next">
    <span class="carousel-control-next-icon" aria-hidden="true"></span>
    <span class="sr-only">Next</span>
  </a>
</div>
</div>


window.onload = function() {
$('#_uploadImages').click(function () {
    $('#_imagesInput').click();

    setTimeout(activeness(), 5000)
});

$('#_imagesInput').on('change', function () {
    handleFileSelect();
});
}


function handleFileSelect() {
    //Check File API support
    if (window.File && window.FileList && window.FileReader) {

        var files = event.target.files; //FileList object
        var output = document.getElementById("carousel-inner");
        var arrFilesCount = [];
        var start = $(output).find('li').length;
        var end = start+ files.length;
        var nonImgCount = 0;
        var slide1 = document.getElementById("slide1")
        var display =  document.getElementById("displayImg")
        for (var i = start; i < end; i++) {
            arrFilesCount.push(i); // push to array
        }
        
        if(start !== 0){
            $(output).find('li > nav > a.prev').first().attr('href','#slide-' + (end-1));
            $(output).find('li > nav > a.next').last().attr('href','#slide-'+start);

            
        }


        
        for (var i = 0; i < files.length; i++) {

            var file = files[i];

            //Only pics
            if (!file.type.match('image')) {nonImgCount++; continue;}

            var picReader = new FileReader();
            picReader.addEventListener("load", function (event) {
                var picFile = event.target;

                current_i = arrFilesCount.shift();
                if (current_i === 0) {
                    prev_i = files.length - 1; //This is for the first element. The previous slide will be the last image. (i=length-1)
                } else {
                    prev_i = current_i - 1;
                }
                if (arrFilesCount.length - nonImgCount === 0) {
                    next_i = 0;
                } else {
                    next_i = current_i + 1; //This is for the last element. The next slide will be the first image (i=0)
                }
                

                display.src = picFile.result

                output.innerHTML = output.innerHTML + '<li id="slide-' + current_i + '" class="carousel-item">' + "<img class='d-block w-100' src='" + picFile.result + "'" + "title=''/>" + '</li>'; // TODO: Enter Title
                
             
                
                
            });
            //Read the image
            picReader.readAsDataURL(file);
            


           
        }
     
       

        if ($("#carousel-inner").find('li')) {
         
          var slide1 = document.getElementById("slide-0");

              slide1.setAttribute ("class", "active");
          
            }
    } else {
        console.log("Your browser does not support File API");
    }
}