2010-08-13 53 views
1

这里是我的HTML类:jQuery的去除。点击

<a href="#">Read more</a> 

<div class="moreDetails"> 
    <p class="additionalText">Some text help here, random length.</p> 
    <p class="author"> 
    <span class="bolder"><a href="minidashboard.php?user_url=http://url.people/1332517">Name</a> 
    </span> 
    </p> 
<div class="replies"> 
    <span> 
    <a href="topic.php?id=http://url/topics/1049198">1</a> 
    </span> 
</div> 

林则使用jQuery类添加到.additionalText股利时,其文字是超过36个字符长。

的jQuery:

$('.moreDetails p.additionalText').filter(function() { 
    if ($(this).text().length > 32) { 
    $(this).addClass('trim'); 
    } 
}); 

<a href="#">Read more</a>点击类.trim被删除,显示的内容我现在想的是。

.trim将段落设置为固定高度,并将溢出设置为隐藏。

+1

只是仅供参考,我并不是说你的代码不起作用,但它有点奇怪的使用'.filter()'。你可能想用'.each()'来代替。或者使用'.filter()'做更多的事情:'$('。moreDetails p.additionalText')。filter(function(){return $(this).text()。length> 32;})。addClass ( '修剪');' – user113716 2010-08-13 15:11:53

回答

5

可以给该链接的一类,像这样:

<a class="readMore" href="#">Read more</a> 

然后添加一个单击处理程序,如:

$("a.readMore").click(function() { 
    $(this).next().find('.additionalText').removeClass('trim'); 
}); 

这是通过寻找<div>相对<a>通过.next().find()。如果<div>立即按照<a>就像在你发布的代码,这可能需要一些调整,例如,如果有在两者之间的元素,但它们仍然是你需要.nextAll('.moreDetails:first')而不是.next()兄弟姐妹。