javascript - 按下按钮时如何显示div的内容而不是值?

标签 javascript jquery html

我希望按钮通过单击按钮一次显示一个 div。我该怎么做?或者我从哪里开始。我是 JavaScript 的新手。到目前为止,我看到的大多数示例都是返回一个 value。下面是代码:

function showDiv(me) {
  /* Disabled buttons accordingly*/
  $('.navbutton').prop('disabled', false);
  $(me).prop('disabled', true);

  /*How do I show the contents of each division only when their respective button is pressed*/

  /*Below is show the value when their respective button is pressed*/
  $('#myDiv').html($(me).val());

}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div id="firstbutton">
  <button class="navbutton" value="home" onclick="showDiv(this);">Table</button>
  <table>
    <tr>
      <th>Column A</th>
    </tr>
    <tr>
      <td>A.1 Introduction</td>
    </tr>
    <tr>
      <td>A.2 History</td>
    </tr>
  </table>
</div>

<div id="secondbutton">
  <button class="navbutton" value="about" onclick="showDiv(this);">Others</button>
  <h2>Miscellaneous items</h2>
</div>

<div id="myDiv"></div>

最佳答案

试试这个

    function showDiv(me) {
        /* Disabled buttons accordingly*/
        $('.navbutton').prop('disabled', false);
        $(me).prop('disabled', true);
        //the below line will get the innerHTML of this clicked elements parent node and place that content in div with id "myDiv"
        $('#myDiv').html(me.parentNode.innerHTML);

    }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div id="firstbutton">
  <button class="navbutton" value="home" onclick="showDiv(this);">Table</button>
  <table>
    <tr>
      <th>Column A</th>
    </tr>
    <tr>
      <td>A.1 Introduction</td>
    </tr>
    <tr>
      <td>A.2 History</td>
    </tr>
  </table>
</div>

<div id="secondbutton">
  <button class="navbutton" value="about" onclick="showDiv(this);">Others</button>
  <h2>Miscellaneous items</h2>
</div>

<div id="myDiv"></div>

关于javascript - 按下按钮时如何显示div的内容而不是值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55487090/

相关文章:

javascript - 无法解决 "Can' t read property X of null”错误

javascript - 在行号nodejs处插入字符串

javascript - 带有触发点击的 fancybox 错误

javascript - 仅在数据表中选择一个复选框行

jquery - 如何修复 Django 中的错误 AttributeError : 'Country' object has no attribute 'City_set' .

javascript - 如何检查是否单击了同一图像或另一图像或图像以外的其他内容

javascript - Jquery 绑定(bind)事件

JavaScript 伪字典

css - 如何禁用特定元素在移动设备上的缩放功能?

html - 水平对齐 div 标题的内容 bootstrap