简体   繁体   English

如何通过单击按钮在html div的中间打印/附加一个值?

[英]How to print / append on the middle of the html div a value from a button click?

On a nav menu with onclick function (example here) that displays a div tab content I would like to pass an additional value to load a specific content. 在具有onclick功能的导航菜单(此处为示例)上 ,该菜单显示div选项卡的内容,我想传递一个附加值来加载特定的内容。 This is the current code: 这是当前代码:

<ul class="tab">
  <li><a href="javascript:void(0)" class="tablinks" onclick="openCity(event, 'London', 'capital')">London</a></li>
  <li><a href="javascript:void(0)" class="tablinks" onclick="openCity(event, 'Paris', 'France')">Paris</a></li>
  <li><a href="javascript:void(0)" class="tablinks" onclick="openCity(event, 'Tokyo', 'Japan')">Tokyo</a></li>
</ul>

<div id="London" class="tabcontent">
  <h3>London</h3>
  <p>London is the <script>document.write(ThirdValue)</script> city of England.</p>
</div>

<div id="Paris" class="tabcontent">
  <h3>Paris</h3>
  <p>Paris is the capital of <script>document.write(ThirdValue)</script>.</p>
</div>

<div id="Tokyo" class="tabcontent">
  <h3>Tokyo</h3>
  <p>Tokyo is the capital of <script>document.write(ThirdValue)</script>.</p>
</div>

And this is the Javascript: 这是Javascript:

function openCity(evt, cityName, ThirdValue) {
  // Declare all variables
  var i, tabcontent, tablinks;

  // Get all elements with class="tabcontent" and hide them
  tabcontent = document.getElementsByClassName("tabcontent");
  for (i = 0; i < tabcontent.length; i++) {
    tabcontent[i].style.display = "none";
   }

  // Get all elements with class="tablinks" and remove the class "active"
  tablinks = document.getElementsByClassName("tablinks");
  for (i = 0; i < tablinks.length; i++) {
    tablinks[i].className = tablinks[i].className.replace(" active", "");
  }

  // Show the current tab, and add an "active" class to the link that opened the tab
  document.getElementById(cityName).style.display = "block";
  evt.currentTarget.className += " active";

  alert(ThirdValue);
}

I am not able to get the third variable within the div. 我无法在div中获得第三个变量。 I am able to see it with alert(ThirdValue); 我可以通过alert(ThirdValue);看到它alert(ThirdValue); , but using <script>document.write(ThirdValue)</script> I am not able to add it within the html. ,但是使用<script>document.write(ThirdValue)</script>我无法将其添加到html中。

I need to add this value within the html. 我需要在html中添加此值。 How can I print / append this value within the html? 如何在html中打印/附加此值?

You shouldn't use document.write , since it is only working while the DOM is loading, after the DOM is ready, it won't work. 您不应该使用document.write ,因为它仅在DOM加载时才起作用,而在DOM准备就绪后,它将无法正常工作。

Instead place a placeholder inside the html (eg: <p>London is the {var} city of England.</p> ), and replace it with ThirdValue . 而是在HTML中放置一个占位符(例如: <p>London is the {var} city of England.</p> ),然后将其替换为ThirdValue Here is how: 方法如下:

var text = document.getElementById(cityName).querySelector('p');
text.innerHTML = text.innerHTML.replace('{var}',ThirdValue);

Full code: 完整代码:

 function openCity(evt, cityName, ThirdValue) { // Declare all variables var i, tabcontent, tablinks; // Get all elements with class="tabcontent" and hide them tabcontent = document.getElementsByClassName("tabcontent"); for (i = 0; i < tabcontent.length; i++) { tabcontent[i].style.display = "none"; } // Get all elements with class="tablinks" and remove the class "active" tablinks = document.getElementsByClassName("tablinks"); for (i = 0; i < tablinks.length; i++) { tablinks[i].className = tablinks[i].className.replace(" active", ""); } // Show the current tab, and add an "active" class to the link that opened the tab document.getElementById(cityName).style.display = "block"; evt.currentTarget.className += " active"; var text = document.getElementById(cityName).querySelector('p'); text.innerHTML = text.innerHTML.replace('{var}',ThirdValue); } 
 <ul class="tab"> <li><a href="javascript:void(0)" class="tablinks" onclick="openCity(event, 'London', 'capital')">London</a></li> <li><a href="javascript:void(0)" class="tablinks" onclick="openCity(event, 'Paris', 'France')">Paris</a></li> <li><a href="javascript:void(0)" class="tablinks" onclick="openCity(event, 'Tokyo', 'Japan')">Tokyo</a></li> </ul> <div id="London" class="tabcontent"> <h3>London</h3> <p>London is the {var} city of England.</p> </div> <div id="Paris" class="tabcontent"> <h3>Paris</h3> <p>Paris is the capital of {var}.</p> </div> <div id="Tokyo" class="tabcontent"> <h3>Tokyo</h3> <p>Tokyo is the capital of {var}.</p> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM