如何更改点击切换? - 在取消绑定按钮上隐藏元素

How to change on click to toggle ? - hide element on unbind button

我得到这个代码(不是我写的)

我无法解决的问题是:

要隐藏该 iframe - 我需要再次点击同一个按钮,我想在点击其他地方时隐藏它 - 比如 toggle()

现在我尝试使用切换创建新代码,但我在获取正确的 ID 时遇到了问题,该 ID 保存在单击的按钮 ID 中,

我还尝试将我的新代码基于此处的答案:

.on() & toggle working togetherjQuery on('toggle') possible?

我的代码 - 当您单击数据表列中的按钮时,它会显示来自动态创建的 link 的 iframe。

$("#example tbody").on("click", "a.actions", function() {
  const id = $(this).attr('href');
  let clicks = $(this).data('clicks');
  if (clicks) {
    $('#' + id).css("display", "none");
  } else {
    $('#' + id).css("display", "block");
  }
  $(this).data("clicks", !clicks);
  let src = 'https://web02.datacentre.local/master-database/master-task-view-actions/entry/' + id;
  $('#' + id).attr('src', src);
  return false;
});

你可以这样做:

$(document).ready(function() {
  $("button").click(function() {
    $("div.d1").toggle();
  });
});
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<button>Toggle</button>

<div class="d1" style="border:1px solid black;padding:10px;width:250px">
  <p><b>This is a little poem: </b><br/> Twinkle, twinkle, little star<br/> How I wonder what you are<br/> Up above the world so high<br/> Like a diamond in the sky<br/> Twinkle, twinkle little star<br/> How I wonder what you are</p>
</div>