简体   繁体   English

有人可以告诉我为什么这不起作用吗? (javascript html 节点选择器)

[英]Can someone tell me why this doesn't work? (javascript html node selector)

This query selector doesn't work on this HTML.此查询选择器不适用于此 HTML。 It works in CSS.它适用于 CSS。 Can anyone tell me the correct solution?谁能告诉我正确的解决方案?

Basically, I am using an older version of the material table and want to hide the "Export as PDF" option.基本上,我使用的是旧版本的材料表,并希望隐藏“导出为 PDF”选项。 I know the newer version allows it in exportButton option.我知道较新的版本允许它在 exportButton 选项中。

<div
  class="MuiPaper-root MuiMenu-paper MuiPopover-paper MuiPaper-elevation8 MuiPaper-rounded"
  tabindex="-1"
  style="
    opacity: 1;
    transform: none;
    transition: opacity 215ms cubic-bezier(0.4, 0, 0.2, 1) 0ms,
      transform 143ms cubic-bezier(0.4, 0, 0.2, 1) 0ms;
    top: 181px;
    left: 1754px;
    transform-origin: 0px 19.5px;
  "
>
  <ul
    class="MuiList-root MuiMenu-list MuiList-padding"
    role="menu"
    tabindex="-1"
  >
    <li
      class="MuiButtonBase-root MuiListItem-root MuiMenuItem-root MuiMenuItem-gutters MuiListItem-gutters MuiListItem-button"
      tabindex="0"
      role="menuitem"
      aria-disabled="false"
    >
      Export as CSV<span class="MuiTouchRipple-root"></span>
    </li>
    <li
      class="MuiButtonBase-root MuiListItem-root MuiMenuItem-root MuiMenuItem-gutters MuiListItem-gutters MuiListItem-button"
      tabindex="-1"
      role="menuitem"
      aria-disabled="false"
    >
      Export as PDF<span class="MuiTouchRipple-root"></span>
    </li>
  </ul>
</div>
document.querySelectorAll("ul.MuiMenu-list li:contains('Export as PDF')")[0].remove();

You need JQuery to use the :contains() Selector您需要JQuery才能使用:contains()选择器

$('td:contains("male")')

Also there is no CSS selector targeting on textContent .也没有针对 textContent 的 CSS 选择器 Take a look at full list of CSS3 Selectors查看CSS3 选择器的完整列表

We need another method here:我们这里需要另一种方法:

function querySelectorIncludesText(selector, text) {
    return Array.from(document.querySelectorAll(selector)).find((el) =>
        el.textContent.includes(text)
    );
}

const find = querySelectorIncludesText("li", "Export as PDF");

console.log(find);

Now you have the correct element.现在你有了正确的元素。

To hide an object you should NOT remove that !要隐藏 object,您应该删除它! You can simply change display property of element:您可以简单地更改元素的display属性:

find.style.display = "none";

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有人可以告诉我为什么其中一种JavaScript代码有效而另一种无效吗? - Can someone tell me why one of these JavaScript code works and one doesn't? 有人可以向我解释为什么这个按钮不起作用? - Can someone explain to me why this button doesn't work? Javascript初学者-谁能告诉我为什么我的按钮不起作用? - Javascript beginner - Can anyone tell me why my button doesn't work? 有人可以告诉我为什么这个js函数无法在加载时运行吗? - Can someone please tell me why this js function doesn't run on load? 有人能告诉我为什么我无法将 Javascript 文件加载到单独的 html 文件中吗? - Can someone tell me why im unable to load Javascript files into separate html file? 谁能告诉我为什么这个jQuery脚本不起作用? - Can someone tell me why this jQuery script does not work? 有人能告诉我为什么 .checked 函数没有在 javascript 中触发吗? - Can someone tell me why .checked function isn't firing in javascript? 谁能告诉我为什么这个JavaScript代码没有按顺序排列一个数组? - Can someone tell me why this JavaScript code isn't lining up an array in order? 有人能告诉我为什么这段代码不叫我function吗? - Could someone tell me why this piece of code doesn't call me that function? 有人可以向我解释为什么绑定方法在这些示例中不起作用 - Can someone explain to me why the binding method doesn't work in these examples
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM