【问题标题】:jQuery - hide elements after specific textjQuery - 在特定文本后隐藏元素
【发布时间】:2013-05-14 12:37:59
【问题描述】:

我想在特定文本/第一个 div 的最后一个单词之后隐藏一些元素

<div class="top">here is some text</div>
<table class="middle">
...
</table>
<div class="bottom">...</div>

根据第一个 div 中的“文本”一词隐藏表格(中间)和 div(底部)

【问题讨论】:

标签: javascript jquery


【解决方案1】:

试试

http://jsfiddle.net/EnQym/1/

txtWord = $('.top').text().split('text')[1]
if(txtWord){
alert("div show");
}else{

alert("div hide");
}

【讨论】:

  • [1] 代表什么?
【解决方案2】:

您可以使用:$('.top').text().split(' ').pop() 获得最后一个词,然后在 show/hide 其他元素中添加一些简单的逻辑:

var lastWord = $('.top').text().split(' ').pop();
$('.middle, .bottom').toggle(lastWord == 'text');

【讨论】:

    【解决方案3】:

    例如,如果您的 .top div 文本是隐藏或显示功能必须是:

    var txt = $('.top').text();
    
    if(txt=='hide')
    {
       $('.middle').hide();
       $('.bottom').hide();
    }
    else if(txt=='show')
    {
       $('.middle').show();
       $('.bottom').show();
    }
    

    【讨论】:

      【解决方案4】:

      如果需要查看第一个div的文字并显示相对的div

      
      
          if($(".top").html() == "what you want")
          {
              $(".middle").show();
              $(".bottom").hide();
          }
          else
          {
              $(".bottom").show();
              $(".middle").hide();
          }
      
      

      【讨论】:

        【解决方案5】:
        var lastWord = function (o) {
            return ("" + o).replace(/[\s-]+$/, '').split(/[\s-]/).pop();
        };
        
        if (lastWord($('.top').html()) === "mykeyword") {
            $('.middle,.bottom').hide();
        } else $('.middle,.bottom').show();
        

        【讨论】:

          【解决方案6】:

          让我们有一个你调用的方法

          hideOrShow(){
          if(document.getElementsByClassName('top')[0].innerHtml=="the text you want"){
          document.getElementsByClassName('middle')[0].style.display='none';
          document.getElementsByClassName('bottom')[0].style.display='none';
          }else{
          document.getElementsByClassName('middle')[0].style.display='block';
          document.getElementsByClassName('bottom')[0].style.display='block';
          }
          }
          

          【讨论】:

            猜你喜欢
            • 2012-02-03
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-10-02
            • 2014-05-30
            • 2012-03-03
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多