• 请不要在回答技术问题时复制粘贴 AI 生成的内容
yulihao
V2EX  ›  程序员

求助如何使用 js 脚本(或油猴脚本)点击 class 按钮

  •  
  •   yulihao · Aug 7, 2020 · 4044 views
    This topic created in 2283 days ago, the information mentioned may be changed or developed.
    按钮代码如下
    <div class="click-area"></div>
    <div class="option-item first-option">A</div>
    要求是当按钮出现时实现自动点击,谢谢啦
    Supplement 1  ·  Aug 10, 2020
    #11 楼
    正解
    no1xsyzy
    no1xsyzy
    Aug 7, 2020❤️1
    1
    当按钮出现时?
    那只有 MutationObserver 了
    yulihao
    yulihao
    OP
    Aug 7, 2020
    2
    @no1xsyzy 有具体代码吗,写第二个按钮就行
    no1xsyzy
    no1xsyzy
    Aug 7, 2020
    3
    @yulihao #2 伸手还是退避吧
    yulihao
    yulihao
    OP
    Aug 7, 2020
    4
    @no1xsyzy 我主要是百度和谷歌了一堆代码(自己也学过来改),挂在油猴里面无效,各种报错
    ochatokori
    ochatokori
    Aug 7, 2020 via Android
    6
    类选择器
    kuro1
    kuro1
    Aug 7, 2020
    7
    $("body").bind('DOMNodeInserted', function(e)
    wlor
    wlor
    Aug 7, 2020
    8
    $("body").bind('DOMNodeInserted', (e) =>{
    if ($(e.target).attr("class") === "option-item first-option") {
    $(e.target).click()
    $('body').unbind("DOMNodeInserted");
    }
    });
    no1xsyzy
    no1xsyzy
    Aug 7, 2020❤️1
    9
    @kuro1 #7 @wlor #8 提醒一下吧
    Note: Mutation Events (W3C DOM Level 3 Events) have been deprecated in favor of Mutation Observers (W3C DOM4).
    顺便 bind / unbind 也 deprecated 了(被 on / off 取代)。
    如果不在乎 deprecated 的话有比 #8 更好的写法。
    提示:1. jquery on 事件代理机制; 2. 为 function 添加名称来在内部使用之。
    wlor
    wlor
    Aug 7, 2020
    10
    好的 学习了
    pluszone
    pluszone
    Aug 7, 2020
    11
    document.querySelector('#xxxxxxx').click();
    可以用 setTimeout 写个延时
    yulihao
    yulihao
    OP
    Aug 10, 2020
    12
    #11 楼
    正解
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   715 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 22:31 · PVG 06:31 · LAX 14:31 · JFK 17:31
    ♥ Do have faith in what you're doing.