简体   繁体   English

父div为hasClass()时显示跨度

[英]Show span if parent div hasClass()

I'm beating myself over the head with this one. 我正用这个打败自己。 I don't know what's doing on. 我不知道在做什么。 It's pretty simple but it just doesn't work. 这很简单,但它不起作用。 I want to show a span item when a div has a class . 我想在div有一个class时显示一个span项。 Yup, basic, but I can't get it to work. 是的,基本的,但我不能让它工作。

Here is my HTML with the class add-product 这是我的带有add-product类的HTML

<div id="ot-pr" class="status-publish has-post-thumbnail hentry add-product">
    <span class="arrow">Testing</span>
</div>

And this is the JavaScript 这是JavaScript

$(document).ready(function($){
    if ($("#ot-pr").hasClass('add-product')){
        $(".arrow").show();
    } else { 
        $(".arrow").hide();
    }
});

Here is the CSS of the .arrow 这是.arrow的CSS

.arrow {
    display: none;
    width: 0; height: 0;
    line-height: 0;
    border-right: 16px solid transparent;
    border-top: 10px solid #c8c8c8;
    top: 60px;
    right: 0;
    position: absolute;
}

What have I tried, adding a find(span) and add else if : 我尝试了什么,添加一个find(span)并添加else if

$(document).ready(function(){
    if ($("#ot-pr").hasClass('add-product')){
        $(".arrow").find('span').show();
    } else if { 
        $(".arrow").hide();
    }
});

Both separate or together don't work. 单独或一起不起作用。 Why isn't this working. 为什么这不起作用。 This should be basic right?! 这应该是基本的吗?! I get no console errors and jQuery.js as been added to the page. 我没有得到控制台错误和jQuery.js已添加到页面中。 All other scripts work just fine. 所有其他脚本工作得很好。

There's no need for JS here, you can achieve this in CSS alone: 这里不需要JS,你可以在CSS中实现这一点:

 .arrow { display: none; /* simplified CSS for the example */ } div.add-product .arrow { display: block; } 
 <div id="ot-pr" class="status-publish has-post-thumbnail hentry add-product"> <span class="arrow">arrow 1</span> </div> <div id="ot-pr" class="status-publish has-post-thumbnail hentry add-product"> <span class="arrow">arrow 2</span> </div> <!-- not visible --> <div id="ot-pr" class="status-publish has-post-thumbnail hentry"> <span class="arrow">arrow 3</span> </div> 

I think no need to write javascript for this, you can manage it by CSS code only 我认为不需要为此编写javascript,您只能通过CSS代码进行管理

.arrow {
    display: none;
    width: 0; height: 0;
    line-height: 0;
    border-right: 16px solid transparent;
    border-top: 10px solid #c8c8c8;
    top: 60px;
    right: 0;
    position: absolute;
}

div.add-product > span.arrow
{
   display: block !important;
}
$(document).ready(function(){

  if($("#ot-pr").hasClass('add-product') == true){
    $(".arrow").css("display","block");
  }
  else { 
    $(".arrow").css("display","none");
    }
});

jQuery solution: jQuery解决方案:

 var $arrow = $('.arrow'), $otPr = $('#ot-pr'); $otPr.hasClass('add-product') ? $arrow.show() : $arrow.hide(); 
 .arrow { display: block; /* ... */ } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="ot-pr" class="status-publish has-post-thumbnail hentry add-product"> <span class="arrow">Some text</span> </div> 

replace css with 用。替换css

.arrow {
    display: block;
    width: 100px; height: 100px;
    line-height: 0;
    border-right: 16px solid transparent;
    border-top: 10px solid #c8c8c8;
    top: 60px;
    right: 0;
    position: absolute;
}

just add display:block and add height width 只需添加display:block并添加高度宽度

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

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