jquery 切换 html 文本

发布于 2024-10-10 05:35:19 字数 930 浏览 1 评论 0原文

我试图显示一个由 a 标签点击触发的隐藏 div:

按钮代码如下所示:

<a href="#" onclick="return false;"  class="viewMoreProducts">View Related Products</a>

这是我单击按钮时的 jquery:

// Show Hidden Product Boxes on Expand - More Products and More Link
$(".viewMoreProducts").click(function() {
    $(this).parent().parent().parent().find(".moreProductsBox:first").slideToggle(300, function (){
        if ($(this).parent().find(".moreProductsBox").is(":visible")){ 
            $(this).find(":input").addClass("visibleCounter");

        }
        if ($(this).parent().find(".moreProductsBox").is(":hidden")){ 
            $(this).find(":input").removeClass("visibleCounter");
        }
    });
});

我可以让隐藏 div 的切换正常工作,但我也想要更改链接上的文本以在显示 div 时更改,然后显示它具有“隐藏相关产品”并显示不同的图标以折叠。然后,如果我折叠它,它会返回阅读“查看相关产品”并显示加号图标。

有什么方法可以将它添加到我已有的 jquery 中吗?

这听起来很简单,但却让我感到困惑。

谢谢

I am trying to show a hidden div that is triggered by an a tag click:

Button Code Looks like this:

<a href="#" onclick="return false;"  class="viewMoreProducts">View Related Products</a>

Here is my jquery for when i click the button:

// Show Hidden Product Boxes on Expand - More Products and More Link
$(".viewMoreProducts").click(function() {
    $(this).parent().parent().parent().find(".moreProductsBox:first").slideToggle(300, function (){
        if ($(this).parent().find(".moreProductsBox").is(":visible")){ 
            $(this).find(":input").addClass("visibleCounter");

        }
        if ($(this).parent().find(".moreProductsBox").is(":hidden")){ 
            $(this).find(":input").removeClass("visibleCounter");
        }
    });
});

I can get the toggle of the hidden div working just fine but i also want to change the text on the a link to change when the div is showing, to then show it has "Hide Related Products" and show a different icon to collapse. Then if i collapse it, it goes back to read View Related Products and has the plus icon.

Any way to just add it to the jquery that i have already?

This sounds pretty straight forward but giving me the run around.

THanks

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

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

发布评论

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

评论(3

谢绝鈎搭 2024-10-17 05:35:19

你可以尝试这样的事情(假设多个parent()不能进一步简化):

$(".viewMoreProducts").click( function(){
  var that = this; // reference to the <a>
  $(this).parent().parent().parent()
      .find(".moreProductsBox:first").slideToggle(300, function (){
        var isvisible = $(this).parent().find(".moreProductsBox").is(":visible");
        $(this).find(":input").toggleClass('visibleCounter', isvisible); 
        $(that).html( (isvisible ? 'Hide It' : 'Show It') );
      });
});

You could try something like this (assuming the multiple parent()s can't be simplified further):

$(".viewMoreProducts").click( function(){
  var that = this; // reference to the <a>
  $(this).parent().parent().parent()
      .find(".moreProductsBox:first").slideToggle(300, function (){
        var isvisible = $(this).parent().find(".moreProductsBox").is(":visible");
        $(this).find(":input").toggleClass('visibleCounter', isvisible); 
        $(that).html( (isvisible ? 'Hide It' : 'Show It') );
      });
});
明媚如初 2024-10-17 05:35:19
<a href="#" class="viewMoreProducts">View Related Products</a>

jQuery:

$(".viewMoreProducts").click(function() {
    var $button = $(this);
    $(this).parent().parent().parent().find(".moreProductsBox:first").slideToggle(300, function () {
        var $input = $(this).find(":input");
        if ($(this).is(":visible")) {
            $input.addClass("visibleCounter");
            $button.text('Hide Related Products');
        } else {
            $input.removeClass("visibleCounter");
            $button.text('View Related Products');
        }
    });
    return false; // allows you to remove that onclick attribute
});
<a href="#" class="viewMoreProducts">View Related Products</a>

jQuery:

$(".viewMoreProducts").click(function() {
    var $button = $(this);
    $(this).parent().parent().parent().find(".moreProductsBox:first").slideToggle(300, function () {
        var $input = $(this).find(":input");
        if ($(this).is(":visible")) {
            $input.addClass("visibleCounter");
            $button.text('Hide Related Products');
        } else {
            $input.removeClass("visibleCounter");
            $button.text('View Related Products');
        }
    });
    return false; // allows you to remove that onclick attribute
});
诗化ㄋ丶相逢 2024-10-17 05:35:19

您可能会发现这有点过时:

var x = this.innerHTML;
this.innerHTML = x.replace("show","z").replace("hide","show").replace("z","hide");

You may find this a bit old fashioned:

var x = this.innerHTML;
this.innerHTML = x.replace("show","z").replace("hide","show").replace("z","hide");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文