Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
154 views
in Technique[技术] by (71.8m points)

javascript - Slider content not showing before click on Next or previous button

Here is a text slider and content of slider we can add through the given text box, when we add multi-lines through the text box then all lines one by one adding to different different slides and we can get it from Next or previous button. but when i add content to slider by textbox then the first time content not show directly, here i need to once click on next or previous button, then the slider content show. Right ?

But i want that the first slide of slider must show instantly when we add content through text box. Can i know what mistake am making plz make it solve.

Here is my All Code.

<button class="prev" onclick="plusSlides(-1)">Previous</button>
<button class="next" onclick="plusSlides(1)">Next</button>
<br>

<textarea class="input" id="input" placeholder="Message..."></textarea>
<button class="waves-effect waves-light" id="send-btn">Send</button>
<br> <br>

<div class="detailcontainer">

</div>

<style>
.detail-items {
display: none;
}

.detailcontainer {
background : yellow;
width: 200px;
}

</style>


<script>
var slideIndex = 1;
showSlides(slideIndex);

function plusSlides(n) {
  showSlides(slideIndex += n);
}

function currentSlide(n) {
  showSlides(slideIndex = n);
}

function showSlides(n) {
  var i;
  var slides = document.getElementsByClassName("detail-items");
  var dots = document.getElementsByClassName("dot");
  if (n > slides.length) {slideIndex = 1}    
  if (n < 1) {slideIndex = slides.length}
  for (i = 0; i < slides.length; i++) {
      slides[i].style.display = "none";  
  }
  for (i = 0; i < dots.length; i++) {
      dots[i].className = dots[i].className.replace(" active", "");
  }
  slides[slideIndex-1].style.display = "block";  
  dots[slideIndex-1].className += " active";
}
</script>


<script>

const sendButton = document.getElementById('send-btn');
const textArea = document.getElementById('input');
const innerDiv = document.getElementsByClassName('detailcontainer')[0];
var message = textArea.value;

sendButton.addEventListener('click', function() {
  // split the textarea entries into an array
  let lines = (textArea.value).split("
");

  // iterate over each line, creating a div/span and inserting into the DOM
  lines.forEach( (line) => {
    let encodedLine = encodeHtmlEntity(line);
    let newElement = `<div class="detail-items">${encodedLine}</div>`;
    innerDiv.innerHTML += newElement;
  });
  
  // reset the textarea
  textArea.value = '';

});

function encodeHtmlEntity(input) {
  var output = input.replace(/[u00A0-u9999<>&]/gim, function(i) {
    return '&#' + i.charCodeAt(0) + ';';
  });

  return output;
}

</script>
question from:https://stackoverflow.com/questions/65952287/slider-content-not-showing-before-click-on-next-or-previous-button

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...