简体   繁体   English

我不知道为什么这个 class 没有通过 javascript 添加

[英]I don't know why this class is not adding through javascript

I am trying to add a javascript class show with dropdown-content, but it is not adding there infact while console logs are working perfectly fine upto the last scope of the javascript script tag. I am trying to add a javascript class show with dropdown-content, but it is not adding there infact while console logs are working perfectly fine upto the last scope of the javascript script tag. Can anyone help me out from this?谁能帮我解决这个问题?

The text with the id is basically coming from django database which is unique.带有 id 的文本基本上来自唯一的 django 数据库。

<div class="eps_dots more_dropdown dropdown">
  <a href="#" onclick="get()" id="{{course.course_id}}" class=""><i class="uil uil-ellipsis-v"></i></a>
  <div class="dropdown-content ">
    <span><i class="uil uil-heart"></i>Save</span>
    <span><i class="uil uil-ban"></i>Not Interested</span>
    <span><i class="uil uil-windsock"></i>Report</span>
  </div>
</div>
<script>
  function get() {
    const focused = document.activeElement.getAttribute("id");
    console.log(focused);

    menu(focused);
  }

  function menu(focused) {
    const path = '#' + focused + ' .dropdown-content';
    console.log(path);

    $(path).toggleClass("show");
  }
</script>                     
.eps_dots .show{
  display: block !important;
}

Given that you're using jQuery, you should not be retrieving the id attribute of the clicked element as a string to manually concatenate a selector together.鉴于您使用的是 jQuery,您不应该将单击元素的id属性作为字符串检索以手动将选择器连接在一起。

In addition, you should be using unobtrusive event handlers to bind your events, not inline onclick attributes.此外,您应该使用不显眼的事件处理程序来绑定您的事件,而不是内联onclick属性。 This would allow you to get a reference to the clicked element from the event that's passed to the handler as an argument.这将允许您从作为参数传递给处理程序的事件中获取对单击元素的引用。 From there you can traverse the DOM to find the .dropdown-content to toggle the relevant class.从那里您可以遍历 DOM 以找到.dropdown-content以切换相关的 class。

 jQuery($ => { $('.eps_dots > a').on('click', e => { e.preventDefault(); $(e.currentTarget).closest('.eps_dots').find('.dropdown-content').toggleClass('show'); }); });
 .dropdown-content { display: none; }.eps_dots.show { display: block; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script> <div class="eps_dots more_dropdown dropdown"> <a href="#" class=""> Click here... <i class="uil uil-ellipsis-v"></i> </a> <div class="dropdown-content"> <span><i class="uil uil-heart"></i>Save</span> <span><i class="uil uil-ban"></i>Not Interested</span> <span><i class="uil uil-windsock"></i>Report</span> </div> </div>

In the "menu" function you have used the given path does not exist because the id would not be parent of the dropdown-content, but instead it would be it's sibling so it would never work.“菜单” function 中,您使用的给定路径不存在,因为 id 不是下拉内容的父级,而是它的兄弟,所以它永远不会工作。

For your code to work the .dropdown-content should be wrapped inside the anchor tag like this为了使您的代码正常工作, .dropdown-content应该像这样包装在锚标记

    <a href="#" onclick="get()" id="{{course.course_id}}" class=""><i class="uil uil-ellipsis-v"></i>
     <div class="dropdown-content ">
      <span><i class="uil uil-heart"></i>Save</span>
      <span><i class="uil uil-ban"></i>Not Interested</span>
      <span><i class="uil uil-windsock"></i>Report</span>
     </div>
    </a>

But as this isn't the case you should try using the sibling format但由于情况并非如此,您应该尝试使用兄弟格式

let current = document.querySelector('#' + focused);
let nextSibling = current.nextElementSibling;

Here the nextSibling would be your dropdown-content element这里 nextSibling 将是您的下拉内容元素

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

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