Javascript 如果跨距包含少于3个字符,则使用jQuery隐藏跨距父项
这让我快发疯了。。。但我确实错过了一些东西 因此HTML看起来像:Javascript 如果跨距包含少于3个字符,则使用jQuery隐藏跨距父项,javascript,jquery,html,text,contains,Javascript,Jquery,Html,Text,Contains,这让我快发疯了。。。但我确实错过了一些东西 因此HTML看起来像: <ul> <li><span>Product spec name</span><span>232112412</span></li> <li><span>Product spec name</span><span>cm</span></li> <li>
<ul>
<li><span>Product spec name</span><span>232112412</span></li>
<li><span>Product spec name</span><span>cm</span></li>
<li><span>Product spec name</span><span>80 cm</span></li>
<li><span>Product spec name</span><span>75 cm</span></li>
<li><span>Product spec name</span><span>cm</span></li>
</ul>
因此,我想要实现的是隐藏第二个跨度包含小于或等于2个字符的列表元素。
我考虑将它们放入一个变量中,循环它们,如果当前项的长度小于或等于2,那么jQuery应该隐藏其父项
下面是我写的代码:
$(document).ready(function () {
var pspec = $('ul li span:nth-child(2)');
for(i=0;i<pspec.length;i++) {
if($(pspec[i]).text().length <= 2) {
$(this).parent().hide();
}
}
});
但是这个代码不会起作用。。。我仍然认为自己是一个jQuery初学者,所以请你能帮我解决这个问题吗?
提前谢谢
致以最良好的祝愿,
Matt您可以分别使用jQuery,而不是使用for并混合使用jQuery和javascript
$(document).ready(function(){
var pspec = $('ul li span:nth-child(2)').each(function(){
if($(this).text().length <= 2) {
$(this).parent().hide();
}
});
});
您可以分别使用jQuery,而不是使用for并混合使用jQuery和javascript
$(document).ready(function(){
var pspec = $('ul li span:nth-child(2)').each(function(){
if($(this).text().length <= 2) {
$(this).parent().hide();
}
});
});
试试下面
$(document).ready(function(){
$.each ($('ul li'), function (idx, el) {
var $span = $(this).find('span').eq(1); //2nd span
if ($span.text().length <= 2) {
$span.parent().hide();
}
});
});
试试下面
$(document).ready(function(){
$.each ($('ul li'), function (idx, el) {
var $span = $(this).find('span').eq(1); //2nd span
if ($span.text().length <= 2) {
$span.parent().hide();
}
});
});
使用过滤器功能
$('ul li span:nth-child(2)').filter(function() {
return $(this).text().length < 3; // <-- get 2nd span elements whose text length < 3
}).parent().hide(); // <-- hide parent elements of the returned elements
使用过滤器功能
$('ul li span:nth-child(2)').filter(function() {
return $(this).text().length < 3; // <-- get 2nd span elements whose text length < 3
}).parent().hide(); // <-- hide parent elements of the returned elements
演示:
由此
$(pspec[i]).parent().hide();
演示:
由此
$(pspec[i]).parent().hide();