【问题标题】:jquery show/hide with post dom link need to add another linkjquery show/hide with post dom link 需要添加另一个链接
【发布时间】:2011-07-18 20:16:57
【问题描述】:

我有一个带有 jquery 显示/隐藏效果和“readmore”按钮的定义列表,一旦打开,它就会变为“关闭”。我现在需要在屏幕顶部为每个描述添加另一个引用锚点的按钮。当我尝试编辑脚本以添加新链接时,“返回页面顶部”按钮然后变成另一个“关闭”按钮。我在 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;}

链接

www.doctorhtiller.com/procedures.html

【问题讨论】:

    标签: jquery hyperlink hide add show


    【解决方案1】:

    这是因为您正在使用此操作元素中的所有锚点:

    $targetSlide.next().children('a').html('Read More');
    

    $targetSlide.next().children('a').html('Close');
    

    children('a') 代码说“让我们影响作为next() 元素的直接子元素的所有锚点”。由于您的“返回页面顶部”锚只是另一个锚,因此它的 html 分别变成了“阅读更多”或“关闭”。您需要做的是将您的锚点与脚本创建的锚点区分开来。我们可以通过向原始锚点添加一个类来轻松做到这一点。

    因此,我们需要为它增添趣味,而不是仅仅添加 &lt;a href="#"&gt;。变化:

    $(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');
    

    【讨论】:

    • 太棒了。像冠军一样工作。甚至没想过要操纵班级……我在想大事,重新配置混乱的脚本。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 2015-09-04
    • 1970-01-01
    • 2016-07-17
    • 1970-01-01
    • 2019-03-22
    • 1970-01-01
    相关资源
    最近更新 更多