使用另一个段落的 link 切换段落

Toggle paragraph using link from another paragraph

我有类似的代码。除了我的 JS 文件是外部的这一事实。函数 myFunction() 在该文件的开头被调用,如下所示:

document.addEventListener('DOMContentLoaded', function () {
  myFunction()
})

但是,它不起作用 - 当单击第一段中的“此处”一词时,它不显示第二段中的文本。我想也许我应该以其他方式调用该函数。任何给定的想法都会非常有帮助。谢谢。

P.S。此外,单击时,window 应滚动到新打开的段落。

function myFunction() {
  var x = document.getElementById("myCollapsible2")
  
  if (x.style.display === "block") {
    x.style.display = "none";
  } else {
    x.style.display = "block";
  }
}
p{
    background: #eee;
    border: 1px solid #ccc;
    margin: 20px 0;
    padding: 30px;
}
.bs-example{
 margin: 20px;
}
.link-color {
color: red;
}
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js">.</script>
        <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
    </head>
    <body>
        <div class="bs-example">
            <input type="button" class="btn btn-primary" data-toggle="collapse" data-target="#myCollapsible" value="Toggle Button">
            <div id="myCollapsible" class="collapse"><p>This is a simple example of expanding and collapsing individual element via data attribute. <br>Click on the <b>Toggle Button</b> button to see the effect. If you click <a onclick="myFunction()" class="link-color" href="#">here</a>, you should see the other paragraph. </p></div>
            <div id="myCollapsible2" class="collapse"><p>This is a simple example</p></div>
        </div>
    </body>
</html>          

因为你用的是bootstrap和popper,你的函数是不需要的,按照documentation. To scroll to the element, use the scrollIntoView的方法设置值即可。我不得不将它包裹在一个 setTimeout 周围,因为默认方法是在之后执行的,所以当我调用 scroll 时,该元素还不可见。

function scrollTo2() {
  var el = document.getElementById('myCollapsible2');
  window.setTimeout(() => el.scrollIntoView(), 0);
}
p {
  background: #eee;
  border: 1px solid #ccc;
  margin: 20px 0;
  padding: 30px;
}

.bs-example {
  margin: 20px;
}

.link-color {
  color: red;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>

<div class="bs-example">
  <input type="button" class="btn btn-primary" data-toggle="collapse" 
    data-target="#myCollapsible" value="Toggle Button">
  <div id="myCollapsible" class="collapse show">
    <p>This is a simple example of expanding and collapsing individual element 
      via data attribute.<br>Click on the <b>Toggle Button</b> 
      button to see the effect. 
      If you click <a onclick="scrollTo2()" data-toggle="collapse" 
      data-target="#myCollapsible2" class="link-color" href="#">here</a>,
      you should see the other paragraph. </p>
    <div id="myCollapsible2" class="collapse">
      <p>This is a simple example</p>
    </div>
  </div>
</div>