单击时,转到另一页并打开一个隐藏的div

时间:2019-01-21 21:36:04

标签: javascript jquery

我有一个FAQ页面,其中列出了所有问题。该列表仅显示问题。单击问题后,答案将隐藏并向下滑动。

在其他页面上,有这些问题的链接。我需要他们打开“常见问题解答”页面并直接显示答案。

我想这与地理位置/定位有关,但我真的不知道如何解决。

这是“常见问题”页面上的HTML

<ul> 
   <li class="#faq1">
      <h1 class="question">
         This is were the question goes.
      </h1>

      <div class="answer">
          This is were the answer goes, only showing when clicking the question or a link on another page linked to this question.
      </div>
   </li>
</ul>

这是jQuery

jQuery(document).ready(function() {
  $(".answer").hide();

  $(".question").click(function()
  {
    $(this).next(".answer").slideToggle(500);
  });
});

在其他一些页面上,我使用普通链接。单击链接时,显然会打开“常见问题”页面,但不会切换答案。

<a href="faq">This is were the question goes</a>

我该怎么做才能从另一个页面触发slideToggle?

2 个答案:

答案 0 :(得分:0)

您可以传递答案以显示在URL的哈希中。

  1. 更改HTML以为每个答案标签包含唯一的ID。
<ul> 
   <li class="#faq1">
      <h1 class="question">
         This is were the question goes.
      </h1>

      <div class="answer" id="answer1">
          This is were the answer goes, only showing when clicking the question or a link on another page linked to this question.
      </div>
   </li>
</ul>
  1. 更改Javascript,以便它获取任何哈希并显示答案。
jQuery(document).ready(function() {
  $(".answer").hide();

  // show the target answer
  if (window.location.hash)
    $(location.hash).slideToggle(500);

  $(".question").click(function()
  {
    $(this).next(".answer").slideToggle(500);
  });
});
  1. 在另一页上更改源链接的href,以包含要显示为哈希的答案的ID。

<a href="faq#answer1">This is were the question goes</a>

答案 1 :(得分:-2)

如果您设置为此使用客户端处理,这是一个解决方案

function GetURLParameter(sParam)
{
    var sPageURL = window.location.search.substring(1);
    var sURLVariables = sPageURL.split('&');
    for (var i = 0; i < sURLVariables.length; i++)
    {
        var sParameterName = sURLVariables[i].split('=');
        if (sParameterName[0] == sParam)
        {
            return sParameterName[1];
        }
    }
}​

要使用,只需致电

var flag = GetURLParameter('goto');

因此,如果您转到网址http://example.com/?goto=answer,则标记将包含“ answer”,并且您将知道要滚动。