如何点击& lt; li& gt; Greasemonkey的商品? [英] How can I click a <li> item with Greasemonkey?

查看:98
本文介绍了如何点击& lt; li& gt; Greasemonkey的商品?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<ul class="myList clearfix" id="thismyList">
    <li class="myBullet" id="answer1">blabla1</li>
    <li class="myBullet" id="answer2">blabla2</li>
    <li class="myBullet" id="answer3">blabla3</li>
</ul>

在此页面中,如何自动单击项目blabla2?

In this page, how can I automatically click item blabla2?

推荐答案

最短,最强大的可能是 XPath 方法(顺便说一句,它是少数w3规范之一,实际上是非常好的和有用的阅读资料).您几乎可以满足任何条件.

The shortest and most powerful is probably the XPath way (btw - it's one of the few w3 specifications that are actually a very good and helpful read). You can have almost any conditions you want to have.

var xresult = document.evaluate("//*[text()='blabla2']", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
xresult.singleNodeValue.click();

evaluate() click()

evaluate(), click()

请参阅此文档,以了解有关JavaScript中XPath的更多信息.

非XPath方式是手动遍历所有节点并搜索包含正确文本的第一个节点:

The non-XPath way would be to go through all the nodes manually and search for the first one containing the right text:

var findElem = function(elems, text) {
    for (var i = 0; i < elems.length; i++) {
        if (elems[i].textContent == text) {
            return elems[i];
        } else {
            var result = findElem(elems[i].children, text);
            if (result != undefined) {
                return result;
            }
        }
    }
    return;
}

findElem(document.documentElement.children, "blabla2").click();

这篇关于如何点击&amp; lt; li&amp; gt; Greasemonkey的商品?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆