【问题标题】:Get href of relatively located element获取相对定位元素的href
【发布时间】:2019-12-06 13:47:14
【问题描述】:

我正在尝试编写一个用户脚本

  1. 在每个超链接后添加一个复选框
  2. 然后,单击复选框后,相应的超链接将其状态更改为“已访问”。 (颜色将从蓝色变为紫色。)

问题是我不明白如何将href 值从a 元素“移动”到desired_element 变量。

为了使示例相对简单,我使用了维基百科。然而,在现实生活中,它适用于不同的 HTML 结构,因此使用 jQuery 可能是一个好主意。 (可能是.closest,然后是.find?)

维基百科案例:

<p>In <a href="/wiki/Computer_programming">computer
programming<input type="checkbox"></a>, a naming convention
is...</p>
<!-- https://en.wikipedia.org/wiki/Naming_convention_(programming) -->

真实案例:

<div>
    <figure>
        <div>
            <div>
                <img src="image.png">
            </div>
            <a href="https://example.com/>Click Me</a>
        </div>
    </div>
    <input type="checkbox">
</div>
// ==UserScript==
// @grant   none
// @match   https://*.wikipedia.org/*
// @name    Wikipedia
// @require https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js
// ==/UserScript==

(function() {
    'use strict';

    function actionFunction() {
        var links = document.querySelectorAll('a');
        var i;
        for (i = 0; i < links.length; i++) {
            var input = document.createElement('input');
            input.setAttribute('type', 'checkbox');
            //input.addEventListener("change", aaa);
            input.onchange = function() {aaa()};
            links[i].appendChild(input);          
        }
    }

    function aaa() {
        var current_url;
        // var desired_url = something?.parentNode.href;

        // store the current URL
        current_url = window.location.href;

        // use replaceState to push a new entry into the browser's history
        history.replaceState({}, '', desired_url);

        // use replaceState again to reset the URL
        history.replaceState({}, '', current_url);
    }

    actionFunction();
})();

【问题讨论】:

标签: javascript jquery tampermonkey userscripts


【解决方案1】:

要完成这项工作,您需要在aaa() 函数中获取对元素的引用。为此,您可以将其作为参数传递,或者您可以使用addEventListener 并在事件处理程序中使用this 来引用引发事件的元素。后者将是更好的做法。

但值得注意的是,a 元素中不能有复选框,因为不能嵌套可点击元素。输入需要是a 的兄弟,这可以通过附加到父级来实现。您还可以将 URL 作为数据属性存储在 input 中,而不必遍历 DOM 来查找相关的 a。试试这个:

function actionFunction() {
  var links = document.querySelectorAll('a');
  var i;
  for (i = 0; i < links.length; i++) {
    var input = document.createElement('input');
    input.type = 'checkbox';
    input.dataset.url = links[i].href;
    input.addEventListener("change", aaa);
    links[i].parentElement.insertBefore(input, links[i].nextSibling);
  }
}

function aaa() {
  let desired_url = this.dataset.url;
  let current_url = window.location.href;
  history.replaceState({}, '', desired_url);
  history.replaceState({}, '', current_url);
}

actionFunction();

【讨论】:

  • 嗯。它给出了非常意想不到的结果:en.wikipedia.org/wiki/Naming_convention_(programming)
  • 我对你的链接感到困惑?
  • 这就是我的意思:i.imgur.com/cNvlOGL.png。第一张来自我的版本,第二张来自你的。
  • 那是因为我的附加到 a 的父级。您将复选框放在 a 中,这是无效的。我更新了代码以在 a 之后插入复选框,而不是附加到父项。
猜你喜欢
  • 1970-01-01
  • 2019-06-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-13
相关资源
最近更新 更多