Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

Javascript Variable Showing instead of Variable's Content


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

1 Answer

0 votes
by (71.8m points)

You try to access an element by accessing a string that holds the name

I think you meant to do this

$(function() {
  const elements = [
    $('<div class="fruit apples">Appels 1</div>'),
    $('<div class="fruit oranges">Oranges 2</div>')
  ]
  const random_element = elements[Math.floor(Math.random() * elements.length)]
  $('.element-placeholder').replaceWith(random_element);

});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="element-placeholder"></div>
<!--This should be one of the .fruit divs instead of the text "pane_2"-->

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...