我有一个带有jquery show / hide效果的定义列表和一个“readmore”按钮,一旦打开就会变为“close”。我现在需要在屏幕顶部为每个描述添加另一个引用锚点的按钮。当我尝试编辑脚本以添加新链接时,“返回页面顶部”按钮将成为另一个“关闭”按钮。我不够精通JS编辑这个脚本,使它做我需要它而没有一些奇怪的后果。任何帮助表示赞赏。
SCRIPT
$(function(){
var slideHeight = 36;
$('.jswrap').each(function(){
var defHeight = $(this).height();
if(defHeight >= slideHeight){
$(this).css({'height':slideHeight,'max-height': defHeight});
$(this).after($('<div class="jsreadmore"><a href="#">Read More</a></div>'));
}
});
$('.jsreadmore a').click(function(){
var $targetSlide = $(this).parent().prev(),
curHeight = $targetSlide.height(),
defHeight = $targetSlide.css('max-height');
if(curHeight == slideHeight){
$targetSlide.animate({
height: defHeight
}, "normal");
$targetSlide.next().children('a').html('Close');
}else{
$targetSlide.animate({
height: slideHeight
}, "normal");
$targetSlide.next().children('a').html('Read More');
}
return false;
});
});
HTML
<div class="jscontainer">
<h4><a id="onedefinition">Definition Text</a></h4>
<div class="jswrap">
<p>content</p>
<p>morecontent</p>
</div></div>
CSS
.content_sub1 .jscontainer {margin:0 auto;}
.content_sub1 .jscontainer h2 {font-size:20px;color:#0087f1;}
.content_sub1 .jswrap {position:relative; padding:10px; overflow:hidden;}
.content_sub1 .jsgradient {width:100%;height:35px; position:absolute; bottom:0; left:0;}
.content_sub1 .jsreadmore {padding:5px; color:#333; text-align:right;}
.content_sub1 .jsreadmore a {padding-right:22px; font-weight:bold; font-size:12px; text-transform: uppercase; color:#c44; font-family:arial, sans-serif;}
.content_sub1 .jsreadmore a:hover {color:#000;}
LINK
www.doctorhtiller.com/procedures.html
答案 0 :(得分:0)
这是因为您使用以下方法操作元素中的所有锚点:
$targetSlide.next().children('a').html('Read More');
和
$targetSlide.next().children('a').html('Close');
children('a')
代码说“让我们影响所有next()
元素的直接子节点”。由于您的“返回页首”锚点只是另一个锚点,因此它的html分别变为“Read More”或“Close”。您需要做的是将锚点与脚本创建的锚点区分开来。我们可以通过向原始锚点添加一个类来轻松完成此操作。
所以,我们不仅要添加<a href="#">
,还需要加强它。变化:
$(this).after($('<div class="jsreadmore"><a href="#">Read More</a></div>'));
到
$(this).after($('<div class="jsreadmore"><a href="#" class="read_more_link">Read More</a></div>'));
现在引用特定的锚元素要容易得多。只需将类添加到选择器代码中:
$targetSlide.next().children('a.read_more_link').html('Read More');
$targetSlide.next().children('a.read_more_link').html('Close');