简体   繁体   English

jQuery Accordion 在展开时自动选择所有包含文本

[英]jQuery Accordion auto-selects all containing text when expanding

I have the following Codepen , showing a simple dropdown I created.我有以下Codepen ,显示了我创建的简单下拉列表。 One will notice that when clicking on the down-arrow at times all the containing text will be selected.人们会注意到,当单击向下箭头时,有时会选择所有包含文本。

How do I stop all the text/content from being auto-selected on active, while leaving the user's option to select text manually?如何阻止所有文本/内容在活动时自动选择,同时让用户选择手动选择文本?

$(document).ready(function() {
  //   Open first panel automatically
  $(".accordion_trigger")[0].nextElementSibling.classList.add("active");
  $(".accordion_trigger")[0].lastElementChild.classList.replace(
    "fa-chevron-down",
    "fa-chevron-up"
  );

  // Click event listener
  $(".accordion_trigger").on("click", function() {
    // Show panel on click
    this.nextElementSibling.classList.toggle("active");

    // update the font-awesome icon up/down
    const fa = this.lastElementChild.classList;
    fa.contains("fa-chevron-down")
      ? fa.replace("fa-chevron-down", "fa-chevron-up")
      : fa.replace("fa-chevron-up", "fa-chevron-down");

    // Remove all other chevron down icons
    const chevrons = $(".fa-chevron-up").filter(
      (index, item) => item != this.lastElementChild
    );
    for (var item of chevrons) {
      item.classList.replace("fa-chevron-up", "fa-chevron-down");
    }

    // Hide all other panels on click
    const others = $(".accordion_trigger").filter(
      (index, item) => item != this
    );
    for (var item of others) {
      item.nextElementSibling.classList.remove("active");
    }
  });
});

In order to replicate this, navigate to the page, then quickly close and re-open the text.为了复制这一点,导航到页面,然后快速关闭并重新打开文本。 Please note that this issue does not occur in Firefox or Edge.请注意,此问题不会在 Firefox 或 Edge 中发生。

<div class="accordion">
  <ul class="accordion_ul">
    <li class="accordion_trigger">
      <h5>Test Entry</h5>
      <i class="fas fa-chevron-down"></i>
    </li>
    <div class="panel">
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
    </div>
    <li class="accordion_trigger">
      <h5>Test Entry</h5>
      <i class="fas fa-chevron-down"></i>
    </li>
    <div class="panel">
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
    </div>
  </ul>
</div>

Add window.getSelection().removeAllRanges();添加window.getSelection().removeAllRanges(); to $(".accordion_trigger").on("click", function(){}$(".accordion_trigger").on("click", function(){}

This will deselect any text you have selected when the panel is displayed (which will be the second click of the double-click that selects everything and triggers opening the panel).这将取消选择您在显示面板时选择的任何文本(这将是双击选择所有内容并触发打开面板的第二次单击)。

I found the solution here: Is there a function to deselect all text using JavaScript?我在这里找到了解决方案: 是否有使用 JavaScript 取消选择所有文本的功能?

Allow me to first explain why this is happening.请允许我首先解释为什么会发生这种情况。

First notice that the panels are positioned absolutely at the top.首先请注意面板绝对位于顶部。 Then notice the visibility and opacity which are initially both set to hidden and 0 respectively and are switched to visible and 1 respectively, that's how the animation is being done.然后注意可见性和不透明度,它们最初分别设置为hidden0并分别切换为visible1 ,这就是动画的完成方式。

Think about the initial panel, it is visible and opaque (opacity 0) and has no absolute position.想想最初的面板,它是visible和不透明的(不透明度 0)并且没有绝对位置。 When you add active class to it(clicking on the chevron) the page becomes absolutely positioned, comes to the top and the opacity takes 0.2s to become 0.当您向其中添加活动类时(单击 V 形),页面将绝对定位,到达顶部,不透明度需要 0.2 秒才能变为 0。

If you look at the specifications for both opacity and visibility: Having opacity value(even 0) makes the element intractable, that's why quickly double clicking the chevron also means double clicking the text inside it.如果您查看不透明度和可见性的规范:具有不透明度值(甚至 0)会使元素难以处理,这就是为什么快速双击 V 形也意味着双击其中的文本。 This only happens on some browsers as others (FF, IE and Edge) will ignore the interactibility since visibility is set to hidden.这仅发生在某些浏览器上,因为其他浏览器(FF、IE 和 Edge)将忽略交互性,因为可见性设置为隐藏。 That's why you'll only notice it if the cursor is placed a little lower than the top.这就是为什么只有当光标放置得比顶部稍低时才会注意到它。 You can easily see this in action if you change the transition rule from transition: visibility 0s, opacity 0.2s linear;如果您将转换规则从transition: visibility 0s, opacity 0.2s linear;更改为transition: visibility 0s, opacity 0.2s linear; to transition: visibility 1s, opacity 0.2s linear; transition: visibility 1s, opacity 0.2s linear; . .

To solve it call window.getSelection().removeAllRanges();要解决它,请调用 window.getSelection().removeAllRanges(); as the first thing in the click event listener.作为点击事件侦听器中的第一件事。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM