javascript - 使用 Chrome 扩展中的 onClick 将按钮添加到页面的正确方法

标签 javascript button google-chrome-extension onclick response

我读过有多种方法可以通过 Chrome 扩展向页面添加按钮,什么是“正确”的方法?

目前我正在这样做:

function notInterested(){
    var notInterestedbutton = $('<div style="text-align:center"><button type="button" style=" margin-top:25px" class="open-in-beamery">Not Interested</button></div>')
    var notInterestedlocation = $('#hiring-platform-promotion-region')
    notInterestedlocation.append(notInterestedbutton)
    notInterestedlocation.on("click", "button", function(){
        console.log('not interested clicked')
        console.log(conversationID)
        chrome.runtime.sendMessage({
            greeting:"notInterested",
            conversationID: conversationID 
        })
    })
}

第二个按钮:

function inmailResponse(){
    var button = $('<button type="button">Add to Response</button><br>')
    var responseLocation = $('#mailbox-main')
    responseLocation.prepend(button)
    responseLocation.on("click", "button", function(){
        console.log('inmail response clicked')
        console.log(conversationID)
        chrome.runtime.sendMessage({
            greeting:"getInmailData",
            conversationID: conversationID 
        })
    })
}

每当我单击 notInterested() 按钮时,它也会触发 inmailResponse() 按钮。为什么?

我该如何编写,以便只有responseLocation按钮被点击?

V2 仍然无法工作:

function notInterested(){
    var notInterestedbutton = $('<div style="text-align:center"><button  type="button" style=" margin-top:25px" class="open-in-beamery">Not Interested</button></div>')
    var notInterestedlocation = $('#hiring-platform-promotion-region')
    notInterestedlocation.append(notInterestedbutton)
    notInterestedbutton.on("click", "button", function(){
        console.log('not interested clicked')
        console.log(conversationID)
        chrome.runtime.sendMessage({
            greeting:"notInterested",
            conversationID: conversationID 
        })
    })
}

function inmailResponse(){
    var inMailButton = $('<button  type="button">Add to Response</button><br>')
    var responseLocation = $('#mailbox-main')
    responseLocation.prepend(inMailButton)
    inMailButton.on("click", "button", function(){
        console.log('inmail response clicked')
        console.log(conversationID)
        chrome.runtime.sendMessage({
            greeting:"getInmailData",
            conversationID: conversationID 
        })
    })
}

两个按钮都被点击

最佳答案

更改处理程序,使它们绑定(bind)到按钮而不是容器

function notInterested(){
  var notInterestedbutton = $('<div style="text-align:center"><button type="button" style=" margin-top:25px" class="open-in-beamery">Not Interested</button></div>')
  var notInterestedlocation = $('#hiring-platform-promotion-region')
notInterestedlocation.append(notInterestedbutton)
notInterestedbutton.on("click", function(){
    console.log('not interested clicked')
    console.log(conversationID)
    chrome.runtime.sendMessage({
        greeting:"notInterested",
        conversationID: conversationID 
    })
 })
}

还有第二个

function inmailResponse(){
 var button = $('<button type="button">Add to Response</button><br>')
 var responseLocation = $('#mailbox-main')
 responseLocation.prepend(button)
 button.on("click", function(){
    console.log('inmail response clicked')
    console.log(conversationID)
    chrome.runtime.sendMessage({
        greeting:"getInmailData",
        conversationID: conversationID 
    })
 })
}    

关于javascript - 使用 Chrome 扩展中的 onClick 将按钮添加到页面的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55289681/

相关文章:

html - CSS - 改变按钮颜色?

javascript - 如何获取当前元素的计算样式

google-chrome-extension - Chrome 运行缓慢,正在访问 Local Storage\chrome-extension_randomname

google-chrome - 为什么 chrome.extension.getBackgroundPage() 返回 null?

javascript - 如何让一个<div>上的 'setTimeout'自动转到另一个<div>?

当文本框填满数据时调用 Javascript 函数

javascript - 使用弹出框选择日期时间?

javascript - 可收缩垫工具栏

css - 使 3 件按钮在 css 中正确组合在一起

java - 如何在javafx中使按钮只按一次