将参数从父函数传递给子函数,javascript

Passing a parameter from a parent to a child function, javascript

  function screenFunc(id) {
      var size = window.matchMedia("(max-width: 620px)")
      if (size.matches) { // If media query matches
        function myFunction(id) {
          var x = document.getElementById(id)
          if (x.style.display === 'none') {
            x.style.display = 'block';
          } else {
            x.style.display = 'none'
          }
        }
      }
    }
<section class="subject">
        <h2 onclick="screenFunc('intro')" class="title-toggle"> Introduction </h2>
        <p id="intro"> Lorem ipsum dolor sit amet, consectetur adipisicing elit. Blanditiis laborum eligendi, nulla aliquid perspiciatis tempore cumque odit sapiente ut, reiciendis accusantium sint dolor. Ipsum eaque, quos esse incidunt, quod veniam sequi sint, voluptatem
          at quis impedit amet illum cupiditate explicabo! </p>
      </section>

我需要在我的 HTML 中访问一个 ID 来切换段落。由于它是移动优先和响应式的,因此只有最大宽度为 620 时才需要切换,所以我想删除 javascript 任何大于该宽度的切换。我可以像这样将 "id" 参数从父函数传递给子函数吗?这是我第一次使用 window.matchmedia,因此感谢反馈。

不需要第二个功能。我认为它可能什么都不做,因为第二个函数从未被调用过。你可以这样做:

function screenFunc(id) {
  var size = window.matchMedia("(max-width: 620px)")
  if (size.matches) { // If media query matches
      var x = document.getElementById(id)
      if (x.style.display === 'none') {
        x.style.display = 'block';
      } else {
        x.style.display = 'none'
      }
    }
}
<section class="subject">
  <h2 onclick="screenFunc('intro')" class="title-toggle"> Introduction </h2>
  <p id="intro"> Lorem ipsum dolor sit amet, consectetur adipisicing elit. Blanditiis laborum eligendi, nulla aliquid perspiciatis tempore cumque odit sapiente ut, reiciendis accusantium sint dolor. Ipsum eaque, quos esse incidunt, quod veniam sequi sint, voluptatem at quis impedit amet illum cupiditate explicabo! </p>
</section>