无法根据 AJAX-driven 页面上的特定 href 删除节点?

Trouble removing nodes based on a specific href, on an AJAX-driven page?

这应该很简单,但我尝试的一切似乎都不起作用。也许我太累了,所以我要求新鲜的眼睛。请帮忙。
在 Greasemonkey 中:检查此确切 link <a href="#?#/3/"> 的页面。如果它在那里,请从视图中删除它的 parents。

我尝试过的东西(几乎所有都可以在另一个堆栈页面上找到)

1 来自 this

// @require http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
$("a[href='#?#/3']").parent().parent().parent().parent().parent().remove();

2

$("a").each(function() {
    if (this.href.indexOf('#?#/3/') != -1) {
        this.parent().parent().parent().parent().parent().parent().remove();
    }
/});

3

$('a').each(function () {
  if ($(this) == 'url("#?#/3/")' {
    $(this).parent().parent().parent().parent().parent().remove();
  }
});

4

var targNode = document.querySelector ("BODY>DIV:nth-of-type(2)>DIV>DIV:nth-of-type(3)>DIV:nth-of-type(2)>DIV:nth-of-type(3)");
var targNodeCheck = targNode.contains("#?#/3");
var targNodeFull = targNode.parent().parent().parent().parent().parent(); 

if (targNodeCheck === true){
  targNodefull.style.display = "none";
}

编辑

我之前没有考虑过,但您确实需要等待页面加载。 (大约 3 秒,有一个 jQuery 加载轮)我不相信这是 Greasemonkey 扩展的问题?

这基本上就是网站的结构。并且有 200 多个初始 div 类 具有不同的 URL 来解析。

<BODY>
    <DIV CLASS="one">
        <DIV CLASS="HOLDER">
            <DIV CLASS="A one">
                <DIV CLASS="IMAGES">
                    <DIV CLASS="LINKHOLDER">
                        <A HREF="#?#/13121/">Link</a>
                        <A HREF="#?#/21231/">Link</a>
                        <A HREF="#?#/3/">Link</a>
                        <A HREF="#?#/41551/">Link</a>
                        <A HREF="#?#/54600/">Link</a>
                        <A HREF="#?#/61650/">Link</a>
                        <A HREF="#?#/72613/">Link</a>
                        <A HREF="#?#/83454/">Link</a>
                        </DIV>

我想你只需要使用 *

$("a[href*='#?#/3']").remove();

我真的不知道你用 parent().parent() 是什么意思......但是如果你需要删除 div 和 class 一个你可以使用

$("a[href*='#?#/3']").closest('.one').remove(); // but this code will remove all the div with links inside

你可以在里面使用你的代码

$(document).ready(function(){
    // your code here
});

您可能需要阅读有关 Selectors

的内容

这是Just Example

如果您看到 jQuery loading wheel 那么该页面肯定是由脚本生成的,这意味着当在 DOMContentLoaded / [=13= 处执行用户脚本时,您需要的元素不存在] 默认事件(大约与 $(document).ready()$(function() { ... }) 相同)

正确的解决方案是定期检查 (setTimeout / setInterval) 元素是否出现或使用 MutationObserver.

使用 setMutationHandler 包装器的示例:

// @require https://greasyfork.org/scripts/12228/code/setMutationHandler.js

setMutationHandler(document, "a[href='#?#/3']", function(nodes) {
    nodes.forEach(function(n) {
        $(n).parents().get(4).remove();
    });
    return true; // continue enumeration of current Mutations batch
});

这是为 AJAX 驱动的页面编写脚本时的典型问题。 waitForKeyElements() 之类的实用程序是专门针对此类问题而设计的。

这是一个完整的脚本,应该适用于问题中给出的代码:

// ==UserScript==
// @name     _YOUR_SCRIPT_NAME
// @include  http://YOUR_SERVER.COM/YOUR_PATH/*
// @require  http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
// @require  https://gist.github.com/raw/2625891/waitForKeyElements.js
// @grant    GM_addStyle
// ==/UserScript==
/*- The @grant directive is needed to work around a design change
    introduced in GM 1.0.   It restores the sandbox.
*/

waitForKeyElements ("a[href='#?#/3/']", removeLinksParents);           

function clickNode (removeLinksParents) {
    jNode.parent ().parent ().parent ().parent ().parent ().remove ();
}