【问题标题】:Hide a parent block (display:none) if child element content text如果子元素内容文本,则隐藏父块(显示:无)
【发布时间】:2020-05-04 23:08:59
【问题描述】:

如果<button> 的子元素有文本<span trans"">Remove</span>,我需要在上下文菜单中隐藏<button> 元素块(显示:无)。

<div id="cdk-overlay-0" class="cdk-overlay-pane context-menu-overlay" style="pointer-events: auto; position: static;">
    <drive-context-menu class="context-menu">
        <context-actions-container>
            <!---->
            <!---->
            <!---->
            <!---->
            <!---->
            <button class="context-menu-item ng-star-inserted">
                <mat-icon class="mat-icon notranslate mat-icon-no-color" role="img" aria-hidden="true">

                    <span trans="">Preview</span>
            </button>
            <!---->
            <!---->
            <!---->
            <!---->
            <!---->
            <!---->
            <!---->
            <button class="context-menu-item ng-star-inserted">
                <span trans="">Aggiungi al mio drive privato</span>
            </button>
            <!---->
            <!---->
            <!---->
            <!---->
            <!---->
            <!---->
            [..........]
        </context-actions-container>
    </drive-context-menu>
</div>

我尝试下一个 css 示例,所有这些规则都隐藏了所有菜单项(按钮),但如果有特定的文本,我不会隐藏一个。

div[id^="cdk-overlay-"] .context-menu context-actions-container button {
    display: none;
}

button.context-menu-item.ng-star-inserted  {
    display: none;
}
.context-menu { 
      display: none;
}

.context-menu-item .ng-star-inserted {
    display: none;
}

【问题讨论】:

标签: javascript css angular css-selectors


【解决方案1】:

你可以display none 使用 jQuery 如果 span 有“删除”,请尝试下面的代码以显示无按钮

<script type="text/javascript">
  $(document).ready(function(){
    $('.context-menu span').each(function () {      
      if($(this).html()==='Remove'){
        $(this).parents('button').css("display","none");
      }
    });   
  })
</script>

以上代码你需要在你的网页中引用jQuery(如果你已经有jquery那么不需要引用)
希望这项工作:)

【讨论】:

  • 我尝试了这个脚本(参考 jQuery 在页面上)但不起作用!
  • 我用页面和元素的代码截图更新了第一条消息...
【解决方案2】:

实现此效果的一种方法是:

  • 在样式表中添加声明:.remove {display: none;}
  • 使用 javascript 搜索正确类型的按钮
  • .remove 类添加到符合所有条件的每个按钮

工作示例:

const contextMenuButtons = [... document.getElementsByClassName('context-menu-item')];

for (contextMenuButton of contextMenuButtons) {

  if (contextMenuButton.getElementsByTagName('span')[0].textContent === 'Remove') {

    contextMenuButton.classList.add('remove');
  }
}
.remove {
  display: none;
}
<button class="context-menu-item ng-star-inserted">
  <span trans="">Aggiungi al mio drive privato</span>
</button>

<button class="context-menu-item ng-star-inserted">
  <span trans="">Remove</span>
</button>

【讨论】:

  • 感谢您的留言..我试试这个脚本,但无法跨度或按钮!
  • 我更新了本主题中的第一条消息以澄清问题...
  • 当你运行上面的代码 sn -p 时,你会看到&lt;button class="context-menu-item ng-star-inserted"&gt;&lt;span trans=""&gt;Remove&lt;/span&gt;&lt;/button&gt; 应用了display: none 样式。
  • 我知道,但在网站上不起作用。脚本在页面中正确加载,但不添加类 .remove 到任何内容..
  • 我发现了问题...您的代码无法正常工作,因为元素
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-08
相关资源
最近更新 更多