在 JavaScript 中,我想将鼠标悬停在单选按钮上并让它输出该单选按钮的值?
我将鼠标悬停在按钮上,它希望将单选按钮的值输出到下面的段落中。我尝试了下面的代码,但它不起作用。我希望“this”作为 ID 意味着激活事件的元素的 ID。
function showDegree() {
var classInput = document.getElementById(this).value;
document.getElementById('classOutput').innerHTML = classInput;
}
I hover over the button and it hopefully outputs the value of the radio button into a paragraph below. I tried the below code but it's not working. I was hoping 'this' as the ID would mean the ID of what element activated the event.
function showDegree() {
var classInput = document.getElementById(this).value;
document.getElementById('classOutput').innerHTML = classInput;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您发布的代码中,“this”指的是当前范围,即函数 showDegree。如果您想引用引发事件的元素:
尝试以下操作:
然后可以像这样调用您的事件侦听器:
In your posted code, "this" refers to the current scope, which would be the function showDegree. If you want to have a reference back to the element that threw the event:
Try this:
Then your event listener can be called like so: