这里是fiddle这是我的代码:
$(document).ready(function(){
$( ".keywordsdiv" ).each(function(){
$(this).children(".keywords").eq(3).after('<a href="" id="playtrailershowmorebuttons">....Show More</a>');//add a unique id to link
$(this).children(".keywords:gt(2)" ).addClass('hide');
});
});
$(document).on('click','a#playtrailershowmorebuttons',function(e){
e.preventDefault();
$(this).addClass('hide');
$(this).parent().children('button.keywords').removeClass('hide');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="keywordsdiv">
<a href="#"><button class="keywords">ABC</button></a>
<a href="#"><button class="keywords">CYZ</button></a>
<a href="#"><button class="keywords">RGRDFS</button></a>
<a href="#"><button class="keywords">FVFVV</button></a>
<a href="#"><button class="keywords">FESF</button></a>
</div>
<div class="keywordsdiv">
<a href="#"><button class="keywords">ABC</button></a>
<a href="#"><button class="keywords">CYZ</button></a>
<a href="#"><button class="keywords">RGRDFS</button></a>
<a href="#"><button class="keywords">FVFVV</button></a>
<a href="#"><button class="keywords">FESF</button></a>
</div>
我希望脚本显示Show More
按钮,如果有超过 3 个带有 keywords
的按钮类(class)div
与类keywordsdiv
,正如您在 fiddle 中看到的那样
如果我删除<a></a>
,则此代码有效代码中的标签
最佳答案
您可以使用 find()
而不是 children()
。因为 keywords
不是 div 的直接子级。
$(document).ready(function() {
$(".keywordsdiv").each(function() {
$(this).find(".keywords").eq(3).after('<a href="" id="playtrailershowmorebuttons">....Show More</a>'); //add a unique id to link
$(this).find(".keywords:gt(2)").addClass('hide');
});
});
$(document).on('click', 'a#playtrailershowmorebuttons', function(e) {
e.preventDefault();
$(this).addClass('hide');
$(this).parent().children('button.keywords').removeClass('hide');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="keywordsdiv">
<a href="#"><button class="keywords">ABC</button></a>
<a href="#"><button class="keywords">CYZ</button></a>
<a href="#"><button class="keywords">RGRDFS</button></a>
<a href="#"><button class="keywords">FVFVV</button></a>
<a href="#"><button class="keywords">FESF</button></a>
</div>
<div class="keywordsdiv">
<a href="#"><button class="keywords">ABC</button></a>
<a href="#"><button class="keywords">CYZ</button></a>
<a href="#"><button class="keywords">RGRDFS</button></a>
<a href="#"><button class="keywords">FVFVV</button></a>
<a href="#"><button class="keywords">FESF</button></a>
</div>
已更新
keywordsdiv
不是button
的直接
父级,因此与closest()
一起使用并提及父类- 并使用
class name
而不是id
附加showmore
元素。并在classname
中单击> - 并且仅删除具有相同类名包含元素的
classname
。它的意思是find('.hide').removeClass('hide')
$(document).ready(function(){
$( ".keywordsdiv" ).each(function(){
$(this).find(".keywords").eq(3).after('<a href="" class="playtrailershowmorebuttons">....Show More</a>');//add a unique id to link
$(this).find(".keywords:gt(2)" ).addClass('hide');
});
});
$(document).on('click','.playtrailershowmorebuttons',function(e){
e.preventDefault();
$(this).closest('.keywordsdiv').find('.hide').removeClass('hide');
$(this).addClass('hide');
});
.hide {
display:none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="keywordsdiv">
<a href="#"><button class="keywords">ABC</button></a>
<a href="#"><button class="keywords">ABC</button></a>
<a href="#"><button class="keywords">ABC</button></a>
<a href="#"><button class="keywords">ABC</button></a>
<a href="#"><button class="keywords">ABC</button></a>
<a href="#"><button class="keywords">ABC</button></a>
<a href="#"><button class="keywords">ABC</button></a>
<a href="#"><button class="keywords">ABC</button></a>
<a href="#"><button class="keywords">ABC</button></a>
</div>
<div class="keywordsdiv">
<a href="#"><button class="keywords">ABC</button></a>
<a href="#"><button class="keywords">ABC</button></a>
<a href="#"><button class="keywords">ABC</button></a>
<a href="#"><button class="keywords">ABC</button></a>
<a href="#"><button class="keywords">ABC</button></a>
<a href="#"><button class="keywords">ABC</button></a>
<a href="#"><button class="keywords">ABC</button></a>
<a href="#"><button class="keywords">ABC</button></a>
<a href="#"><button class="keywords">ABC</button></a>
</div>
关于javascript - "Show More"X按钮后的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45116081/