我正在使用 javascript 将按钮动态插入到文档中,我希望按钮可以单击并获取每个按钮的值并将它们导出到 conosle.log 中。现在我可以点击按钮,但我不知道如何将按钮值从 .on() 传递给函数,有什么建议吗?
html
<button id="testTagButton" class="yt-chip" title="test button please ignore"><span>test button please ignore</span></button>
JavaScript
$(document).on('click','#testTagButton',tagInsert);
function tagInsert(){
console.log("Button Value should go here");
}
我必须使用 .on() afaik,因为我正在使用 append 将按钮插入到文档中
最佳答案
简单地引用上下文(也就是这个):
function tagInsert(){
console.log(
this.id, //a regular DOM prop
this.title,
$(this).text() //or utilizing jquery again
//or whatever
);
}
关于javascript - 我将如何将变量从 .on() 事件传递到回调函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45491252/