jquery 显示/隐藏 post dom 链接需要添加另一个链接

发布于 2024-11-25 01:35:01 字数 2160 浏览 10 评论 0原文

我有一个定义列表,其中包含 jquery 显示/隐藏效果和“阅读更多”按钮,一旦打开,该按钮就会更改为“关闭”。我现在需要在屏幕顶部放置另一个按钮来引用每个描述的锚点。当我尝试编辑脚本以添加新链接时,“返回页面顶部”按钮变成另一个“关闭”按钮。我对 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

I have a list of definitions with a jquery show/hide effect and "readmore" button that changes to "close" once its open. I now need to put another button that references an anchor at the top of the screen for each description. When I tried editing the script to add the new link, the "return to top of page" button then becomes another "close" button. I am not versed enough in JS to edit this script to make it do what I need it to without some strange consequence. Any help is appreciated.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

往日 2024-12-02 01:35:01

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

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

并且

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

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

因此,我们需要为其增添趣味,而不是仅仅添加 。更改:

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

This is because you are manipulating ALL the anchors in your element with this:

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

and

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

The children('a') code says "Let's affect all anchors which are direct children of the next() element". Since your "return to top of page" anchor is simply another anchor, it has its html turned into 'Read More' or 'Close' respectively. What you need to do is differentiate your anchor from the anchors created by the script. We can easily do this by adding a class to the original anchors.

So, instead of just adding an <a href="#"> we need to spice it up. Change:

$(this).after($('<div class="jsreadmore"><a href="#">Read More</a></div>'));

to

$(this).after($('<div class="jsreadmore"><a href="#" class="read_more_link">Read More</a></div>'));

Now it's much easier to reference that specific anchor element. Simply add the class to your selector code:

$targetSlide.next().children('a.read_more_link').html('Read More');
$targetSlide.next().children('a.read_more_link').html('Close');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文