admin管理员组文章数量:1379944
I have double code in WP:
<div class="row top-banner">
<div class="columns">
<section id="text-12" class="widget widget_text"> <div class="textwidget"><img id="bannerFront" class="showRtb" style="cursor:pointer;" src=".png" alt="treat_someone"></div>
</section>
</div>
</div>
I have tried everything from WP backend and decided to use css + JavaScript to solve this. I want to only show the first top-banner. Here is the code to hide it:
.textwidget:nth-of-type(1) {
display:none;
}
When I put that code onto my WP page, both banners disappear not just second.
How can I only hide the second banner and not the first?
I have double code in WP:
<div class="row top-banner">
<div class="columns">
<section id="text-12" class="widget widget_text"> <div class="textwidget"><img id="bannerFront" class="showRtb" style="cursor:pointer;" src="https://www.asdasd./wp-content/uploads/2017/03/treat_someone-1.png" alt="treat_someone"></div>
</section>
</div>
</div>
I have tried everything from WP backend and decided to use css + JavaScript to solve this. I want to only show the first top-banner. Here is the code to hide it:
.textwidget:nth-of-type(1) {
display:none;
}
When I put that code onto my WP page, both banners disappear not just second.
How can I only hide the second banner and not the first?
Share Improve this question edited Mar 20, 2017 at 18:07 mccoyLBI 17114 bronze badges asked Mar 20, 2017 at 16:05 Aleks PerAleks Per 1,6397 gold badges38 silver badges74 bronze badges 3- I can only see one banner image in this HTML code – Simrandeep Singh Commented Mar 20, 2017 at 16:09
- yes but I have this code 2 times in home page ... – Aleks Per Commented Mar 20, 2017 at 16:11
-
it's because your
.textwidget
div is the first of it's type in the.widget
div. If you want a css solution, we need to see where your second banner is in relation to your first, otherwise I would use a js solution – Pete Commented Mar 20, 2017 at 16:13
4 Answers
Reset to default 1nth-of-type
works on siblings, in other words, they need to be children of the same parent. If they are in different parents in the page, nth-
won't work. You could select all elements with class .textwidget
and hide the second one (index 1) with jQuery.
$('.textwidget:eq(1)').hide();
Edit: ...or you could use Vanilla Javascript
// on page ready method definition
function ready(fn) {
if (document.readyState != 'loading'){
fn();
} else {
document.addEventListener('DOMContentLoaded', fn);
}
}
// run the function inside this method when page finished loading
ready(function(){
// hide second element
document.getElementsByClassName('textwidget')[1].style.display = 'none';
})
<div class="row top-banner">
<div class="columns">
<section id="text-12" class="widget widget_text">
<div class="textwidget">
<img id="bannerFront" class="showRtb" style="cursor:pointer;"
src="https://i.vimeocdn./portrait/58832_300x300"
alt="treat_someone"></div>
</section>
</div>
</div>
<div class="row top-banner">
<div class="columns">
<section id="text-12" class="widget widget_text">
<div class="textwidget">
<img id="bannerFront" class="showRtb" style="cursor:pointer;"
src="https://i.vimeocdn./portrait/58832_300x300"
alt="treat_someone"></div>
</section>
</div>
</div>
nth-of-type : selector matches every element that is the nth child, of a particular type, of its parent. Here us the code which is hiding 2nd element of .textwidget
.textwidget:nth-of-type(2) {
display: none;
}
<div class="row top-banner">
<div class="columns">
<section id="text-12" class="widget widget_text">
<div class="textwidget">1st<img id="bannerFront" class="showRtb" style="cursor:pointer;" src="" alt="treat_someone"></div>
<div class="textwidget">2nd<img id="bannerFront" class="showRtb" style="cursor:pointer;" src="" alt="treat_someone"></div>
</section>
</div>
</div>
In Plain javascript, try
document.getElementsByClassName('textwidget')[1].style.display='none';
You are using wrong CSS. I have modified your CSS code, it will work for you.
.top-banner:nth-of-type(1) {
display:none;
}
本文标签: javascriptHide second element with same class or idStack Overflow
版权声明:本文标题:javascript - Hide second element with same class or id - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744471033a2607798.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论