如何防止元素使用jQuery或JavaScript出现?

时间:2018-03-30 11:17:16

标签: javascript jquery css

我在Tampermonkey中编写代码,这是Google Chrome的扩展程序。我尝试了以下但是它没有隐藏元素。

// ==UserScript==
// @name         Minimal YouTube
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Make some changes to YouTube
// @author       You
// @match        https://www.youtube.com/*
// @grant        none
// @require      http://code.jquery.com/jquery-3.3.1.min.js
// ==/UserScript==

(function() {
    'use strict';
    var element = $("#label:contains('Creator Studio')").closest('ytd-compact-link-renderer');
    element.css("display", "none");
})();

以下是来自youtube网页的html。

<ytd-compact-link-renderer class="style-scope yt-multi-page-menu-section-renderer" compact-link-style="">
  <a id="endpoint" class="yt-simple-endpoint style-scope ytd-compact-link-renderer" tabindex="-1" href="/dashboard">
    <paper-item class="style-scope ytd-compact-link-renderer" role="option" tabindex="0" aria-disabled="false">
      <div class="content-icon style-scope ytd-compact-link-renderer">
        <yt-img-shadow height="40" width="40" class="style-scope ytd-compact-link-renderer" disable-upgrade="" hidden="">
        </yt-img-shadow>
        <yt-icon class="style-scope ytd-compact-link-renderer">
          <svg viewBox="0 0 24 24" preserveAspectRatio="xMidYMid meet" focusable="false" class="style-scope yt-icon" style="pointer-events: none; display: block; width: 100%; height: 100%;">
            <g class="style-scope yt-icon">
              <path d="M19.43 12.98c.04-.32.07-.64.07-.98s-.03-.66-.07-.98l2.11-1.65c.19-.15.24-.42.12-.64l-2-3.46c-.12-.22-.39-.3-.61-.22l-2.49 1c-.52-.4-1.08-.73-1.69-.98l-.38-2.65C14.46 2.18 14.25 2 14 2h-4c-.25 0-.46.18-.49.42l-.38 2.65c-.61.25-1.17.59-1.69.98l-2.49-1c-.23-.09-.49 0-.61.22l-2 3.46c-.13.22-.07.49.12.64l2.11 1.65c-.04.32-.07.65-.07.98s.03.66.07.98l-2.11 1.65c-.19.15-.24.42-.12.64l2 3.46c.12.22.39.3.61.22l2.49-1c.52.4 1.08.73 1.69.98l.38 2.65c.03.24.24.42.49.42h4c.25 0 .46-.18.49-.42l.38-2.65c.61-.25 1.17-.59 1.69-.98l2.49 1c.23.09.49 0 .61-.22l2-3.46c.12-.22.07-.49-.12-.64l-2.11-1.65zM10 15V9l5 3-5 3z" class="style-scope yt-icon"></path>
            </g>
          </svg>
        </yt-icon>
      </div>
      <yt-formatted-string id="label" class="style-scope ytd-compact-link-renderer">Creator Studio</yt-formatted-string>
      <yt-formatted-string id="subtitle" class="style-scope ytd-compact-link-renderer"></yt-formatted-string>
      <yt-icon id="right-icon" class="style-scope ytd-compact-link-renderer" disable-upgrade="" hidden="">
      </yt-icon>
      <yt-formatted-string id="secondary-text" class="style-scope ytd-compact-link-renderer"></yt-formatted-string>
    </paper-item>
  </a>
</ytd-compact-link-renderer>

1 个答案:

答案 0 :(得分:3)

.closest()选择一个需要classid或任何tag/attribute

的选择器

所以代码需要

或者: -

.closest('.ytd-compact-link-renderer') // in case of class

或者

.closest('#ytd-compact-link-renderer') // in case of id

或者

.closest('input[name=ytd-compact-link-renderer]') // just an example of attribute

注意: - 而不是.css()直接使用.hide()。更容易。

在您的情况下使用: -

$("#label:contains('Creator Studio')").hide();

工作代码段: -

&#13;
&#13;
(function() {
    'use strict';
    $("#label:contains('Creator Studio')").hide();
})();
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ytd-compact-link-renderer class="style-scope yt-multi-page-menu-section-renderer" compact-link-style="">

    <a id="endpoint" class="yt-simple-endpoint style-scope ytd-compact-link-renderer" tabindex="-1" href="/dashboard">
      <paper-item class="style-scope ytd-compact-link-renderer" role="option" tabindex="0" aria-disabled="false">


        <div class="content-icon style-scope ytd-compact-link-renderer">
          <yt-img-shadow height="40" width="40" class="style-scope ytd-compact-link-renderer" disable-upgrade="" hidden="">
          </yt-img-shadow>
          <yt-icon class="style-scope ytd-compact-link-renderer"><svg viewBox="0 0 24 24" preserveAspectRatio="xMidYMid meet" focusable="false" class="style-scope yt-icon" style="pointer-events: none; display: block; width: 100%; height: 100%;"><g class="style-scope yt-icon">
        <path d="M19.43 12.98c.04-.32.07-.64.07-.98s-.03-.66-.07-.98l2.11-1.65c.19-.15.24-.42.12-.64l-2-3.46c-.12-.22-.39-.3-.61-.22l-2.49 1c-.52-.4-1.08-.73-1.69-.98l-.38-2.65C14.46 2.18 14.25 2 14 2h-4c-.25 0-.46.18-.49.42l-.38 2.65c-.61.25-1.17.59-1.69.98l-2.49-1c-.23-.09-.49 0-.61.22l-2 3.46c-.13.22-.07.49.12.64l2.11 1.65c-.04.32-.07.65-.07.98s.03.66.07.98l-2.11 1.65c-.19.15-.24.42-.12.64l2 3.46c.12.22.39.3.61.22l2.49-1c.52.4 1.08.73 1.69.98l.38 2.65c.03.24.24.42.49.42h4c.25 0 .46-.18.49-.42l.38-2.65c.61-.25 1.17-.59 1.69-.98l2.49 1c.23.09.49 0 .61-.22l2-3.46c.12-.22.07-.49-.12-.64l-2.11-1.65zM10 15V9l5 3-5 3z" class="style-scope yt-icon"></path>
      </g></svg>


  </yt-icon>
        </div>
        <yt-formatted-string id="label" class="style-scope ytd-compact-link-renderer">Creator Studio</yt-formatted-string>
        <yt-formatted-string id="subtitle" class="style-scope ytd-compact-link-renderer"></yt-formatted-string>
        <yt-icon id="right-icon" class="style-scope ytd-compact-link-renderer" disable-upgrade="" hidden="">
        </yt-icon>
        <yt-formatted-string id="secondary-text" class="style-scope ytd-compact-link-renderer"></yt-formatted-string>

  </paper-item>
    </a>
  </ytd-compact-link-renderer>
&#13;
&#13;
&#13;