社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Jquery

jquery样式未应用于其元素

Adrian • 4 年前 • 695 次点击  

给出以下代码。考虑下面的问题,为什么里面有东西 #d1 不是从“命令”中获取样式,我尝试使用 .addClass 也可以显示我的菜单。以下项目是下拉菜单。如果我添加 ("alert("hello");") 它将打印出该消息,尽管它永远不会应用这些样式或类。为什么会这样?

    $('#d1').click(function () {
   $(this).attr('style', 'display: block!important');
});
.dropdown-list {
        display: none;
    }
    .active {
    color: red; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<nav>
            <div class="logo">LOGO</div> 
            <ul>
                <li><a href="#" class="active">Home</a></li>
                <li><a id="d1" href="#">Projects</a>
                <ul class="dropdown-list" onclick="myFunction(this)">                    
                    <li><a style="line-height: 0;" href="#">Pictures</a></li>
                    <li><a style="line-height: 0;" href="#">Movies</a></li>
                    <li><a style="line-height: 0;" href="#">Slow-mo</a></li>
                </ul>
                </li>
                <li><a id="d2" href="#">About me</a>
                    <ul class="dropdown-list">                    
                        <li><a style="line-height: 0;" href="#">CV</a></li>
                        <li><a style="line-height: 0;" href="#">Phone</a></li>
                    </ul>
                    </li>
                <li><a href="#">Contact</a></li>
            </ul>
        </nav>

谢谢。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/37907
 
695 次点击  
文章 [ 2 ]  |  最新文章 4 年前
WebDirection
Reply   •   1 楼
WebDirection    5 年前

你可以试试这个

$('ul li a').click(function (){
   $("ul li > a").removeClass("active");
   $(this).addClass("active");
   if($(this).next('.dropdown-list').length != 0){
        $(this).next('.dropdown-list').slideToggle();
   }
});
.dropdown-list {
        display: none;
 }
.active {
  color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<nav>
   <div class="logo">LOGO</div> 
     <ul>
       <li><a href="#" class="active">Home</a></li>
       <li><a id="d1" href="#">Projects</a>
           <ul class="dropdown-list">                    
                  <li><a style="line-height: 0;" href="#">Pictures</a></li>
                    <li><a style="line-height: 0;" href="#">Movies</a></li>
                    <li><a style="line-height: 0;" href="#">Slow-mo</a></li>
           </ul>
       </li>
       <li><a id="d2" href="#">About me</a>
           <ul class="dropdown-list">                    
                        <li><a style="line-height: 0;" href="#">CV</a></li>
                        <li><a style="line-height: 0;" href="#">Phone</a></li>
           </ul>
       </li>
       <li><a href="#">Contact</a></li>
     </ul>
  </nav>
Taplar
Reply   •   2 楼
Taplar    5 年前

你必须瞄准 next() 你想展示的。

//get a reference to all the links
var $dropdownLinks = $('.dropdown-link');

//bind the click handler to the links.
$dropdownLinks.on('click', function() {
  var $this = $(this);

  //remove the active class from the other links
  $dropdownLinks.not(this).removeClass('active');
  //toggle the active class on this element so it can open or close
  $this.toggleClass('active');
});
//make the link red if active
.dropdown-link.active {
  color: red;
}

//hide the immediately following sibling dropdown-list element
//of the dropdown-link, so long as it is not active
.dropdown-link:not(.active) + .dropdown-list {
  display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<nav>
  <div class="logo">LOGO</div>
  <ul>
    <li><a href="#" class="active dropdown-link">Home</a></li>
    <li><a id="d1" class="dropdown-link" href="#">Projects</a>
      <ul class="dropdown-list" onclick="myFunction(this)">
        <li><a style="line-height: 0;" href="#">Pictures</a></li>
        <li><a style="line-height: 0;" href="#">Movies</a></li>
        <li><a style="line-height: 0;" href="#">Slow-mo</a></li>
      </ul>
    </li>
    <li><a id="d2" class="dropdown-link" href="#">About me</a>
      <ul class="dropdown-list">
        <li><a style="line-height: 0;" href="#">CV</a></li>
        <li><a style="line-height: 0;" href="#">Phone</a></li>
      </ul>
    </li>
    <li><a href="#" class="dropdown-link">Contact</a></li>
  </ul>
</nav>