【问题标题】:Get and search for the hovered link URL "only"获取并搜索“仅”悬停的链接 URL
【发布时间】:2021-05-09 21:25:37
【问题描述】:

顾名思义

「获取并搜索悬停的链接URL“仅”」

要实现这一点需要进行哪些更正?

// Pattern1⃣ Malfunction
// ① Opens all URLs that contain the specified word, regardless of mouse over target.
// ② Opens with or without mouseover.
var ele = document.querySelectorAll(".centerarticle-entry-title a");
var link = ['Loading...', 'Dance Party'];
var L = window.onload = function(load) {
    window.addEventListener('keydown', (e) => {
        if (e.ctrlKey && e.keyCode === 13) { // CTRL + ENTER
            for (let i = 0; i < ele.length; i++) {
                for (let j = 0; j < link.length; j++) {
                    if (!link.onmouseenter) {
                        e.stopPropagation();
                    }
                    if (ele[i].innerHTML.match(link[j])) {
                        ele[i].innerHTML.match(link[j]).onmouseenter = window.open("https://web.archive.org/web/" + ele[i].href);
                        L = undefined;
                        ele[i].onmouseenter = undefined;
                        ele[i].onmouseenter = null;
                    }
                }
            }
        } else {
            ele[i].onmouseleave = e.preventDefault();
            return false;
        }
    });
};

以Pattern1⃣为例,这在另一种意义上是实用方便的,但我们这次要实现的功能与Pattern1⃣不同。

你要实现的功能是 「 获取并搜索悬停的链接 URL "only" 」 我对此很讲究。

我已经创建了一些其他的原型,但它们更不实用,因为模式 1⃣ 的版本更差或者搜索结果为空。

// Pattern2⃣ Malfunction
// ① Opens all URLs that contain the specified word, regardless of mouse over target.
// ② There is a 'case' that opens even if you don't mouse over.
// ③ In some cases, nothing responds in the first place, in which case you need to do a super reload etc. each time.
// ④ The number of times you pressed the ENTER key to the sky may have accumulated. Alternatively, the number of hovering times can be accumulated as it is. That possibility can open duplicate TABs unnecessarily.
var ele = document.querySelectorAll(".centerarticle-entry-title a");
var link = ['Loading...', 'Dance Party'];
document.addEventListener('mouseenter', (m_enter) => {
    document.addEventListener('mouseleave', (m_leave) => {
        m_enter.preventDefault();
        e.preventDefault();
        return false;
    });
    window.addEventListener('keydown', (e) => {
        if (!(e.ctrlKey && e.keyCode == 13)) {
            m_enter.preventDefault();
            return false;
        }
        if (e.ctrlKey && e.keyCode == 13) {
            for (let i = 0; i < ele.length; i++) {
                for (let j = 0; j < link.length; j++) {
                    if (ele.innerHTML.match(link[j])) {
                        link[j].onmouseover = window.open("https://web.archive.org/web/" + ele[i].href);
                        location.reload();
                        break;
                    }
                }
            }
        } else {
            return false;
        }
    });
});


// Pattern3⃣ Malfunction
// ① Opens only one (probably top) URL that contains the specified word, regardless of whether you hover your mouse over the target.
// ② Opens with or without mouseover.
var ele = document.querySelectorAll(".centerarticle-entry-title a");
var link = ['Loading...', 'Dance Party'];
window.addEventListener('keydown', (e) => {
    if (e.ctrlKey && e.keyCode === 13) { // CTRL + ENTER key
        for (let i = 0; i < ele.length; i++) {
            for (let j = 0; j < link.length; j++) {
                if (ele[i].innerHTML.match(link[j])) {
                    link[j].onmouseover = window.open(("https://web.archive.org/web/" + ele[i].href));
                    return false;
                }
            }
        }
    }
});


// Pattern4⃣ Malfunction
// ① Opens with or without mouseover.
// ② Search results are empty or "undefined"
var ele = document.querySelectorAll(":hover");
var link = ['Loading...', 'Dance Party'];
window.addEventListener('keydown', (e) => {
    if (e.ctrlKey && e.keyCode == 13) {
        link.onmouseenter =
            window.open("https://web.archive.org/web/" + this.href);
        return false;
    }
});

实际的实验目标页面如下。

https://b.hatena.ne.jp/search/tag?q=bookmarklet&page=67

请告诉我,

【问题讨论】:

  • 你到底想达到什么目的?
  • 「获取并搜索悬停的链接URL“仅”」
  • 所以你有一个有很多链接的页面,你想悬停一个链接,按 ctrl+enter 然后打开链接?
  • 没错。理想情况下,您应该将鼠标悬停在链接上并按一个键打开它,或者执行ctrl + click 之类的操作来打开它。
  • 非常感谢。我能够通过如下书写来确认所需的操作。 window.addEventListener('keydown', (e) =&gt; { const link = document.querySelector('a:hover'); if (e.ctrlKey &amp;&amp; e.keyCode === 13){ if (link) window.open("https://web.archive.org/web/" + link.href); } });

标签: javascript jquery mouseevent mouseover mouselistener


【解决方案1】:

<a href="https://google.com">Google</a>
<a href="https://yahoo.com">Yahoo</a>
<script>
document.onkeydown = () => {
  const link = document.querySelector('a:hover')
  if (link) window.open(link.href)
}
</script>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-12
    • 2018-11-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多