javascript - 从 Href 打开扩展列表

标签 javascript jquery html

我正在尝试修改 this pen I found on CodePen 。我希望能够从另一个页面打开该页面上的特定列表。单击该链接应在页面加载时打开下一页上的相应部分。

对于 jQuery,我是个新手,所以我很感谢我能得到的任何帮助。我尝试四处寻找并了解我需要实现的目标,但我一直无法实现它。这是我的代码:

HTML:

<!--Link on Previous Page-->
<a href="test/#list">Click Here</a>


<!--Target List-->
<div class="integration-list">
  <ul>
    <li class="integration">
    <a class="expand" id="list">
    <div class="expand_intro"><h3 class="teal_bold">Click Here</h3></div>
    <div class="right-arrow">&#9660;</div>
    </a>

    <div class="detail">
      <div><p>Lorem Ipsum Dolor...</p></div>
    </div>

    </li>
  </ul>
</div>

JS:

$(function() {
$(".expand").on( "click", function() {
  $(this).next().slideToggle(100);
  $expand = $(this).find(">:nth-child(2)");

  if($expand.text() == "▼") {
  $expand.text("▲");
} else {
  $expand.text("▼");
}

var hash = window.location.hash;
var thash = hash.substring(hash.lastIndexOf('#'), hash.length);
$('.expand').find('a[href*='+ thash + ']').trigger('click');

  });
});

最佳答案

为了让它发挥作用,我做了一些事情:

触发事件可能在处理程序实际附加之前触发。您可以使用 setTimeout 作为解决此问题的方法。

此外,即使 $('.expand').find('a[href*='+ thash + ']').trigger('click') 周围有 setTimeout ; 它对我不起作用。我将其更改为简单的 $(thash).click();

“expand.js”文件的完整代码:

$(function() {

    var hash = window.location.hash;
    var thash = hash.substring(hash.lastIndexOf('#'), hash.length);
    setTimeout(function() {
        $(thash).click();
    }, 10);

    $(".expand").on( "click", function() {
    $(this).next().slideToggle(100);
    $expand = $(this).find(">:nth-child(2)");

    if($expand.text() == "â–¼") { //If you copy/paste, make sure to fix these arrows
          $expand.text("â–²");
       } else {
          $expand.text("â–¼");
       }
    });
});

显然,此处的箭头无法正确显示,因此,如果您复制/粘贴此内容,请注意。

关于javascript - 从 Href 打开扩展列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24189990/

相关文章:

javascript - Angular.js 中的 "scope.watch is not a function"错误

javascript - 在 $(document).keypress 函数中使用 $.ajax 后获取非法调用

html - 无法删除图像上的边框 - css

html - 如何防止元素因绝对定位而移动?

php - 多个表单仅重定向第一个表单的操作

javascript - 使用 async/await 时如何获取完整的 Node.js 堆栈跟踪?

javascript - Shadowbox - 如何切换或替换内容?如何关闭和打开另一个对话框?

javascript - 如何将 'Add More' 按钮添加到一组下拉列表中?

javascript - 将 int 数组从 js 发送到 Controller

javascript - jquery中如何获取节点元素的索引