它的工作方式是这样的:当我单击订阅按钮时,它会显示一条消息:
<span id="es_msg">
Your subscription was successful! Kindly check your mailbox and confirm your subscription. If you don't see the email within a few minutes, check the spam/junk folder.</span>
我必须使用 javascript 更改此消息,因为我无权访问 html。我尝试使用下面的代码,但即使在单击订阅按钮之前,“测试”消息也始终可见。
document.getElementById('es_widget_msg').innerHTML='Test';
最佳答案
那是因为您没有将代码设置为 "event handler" 的一部分。一旦遇到,它就会在没有任何用户参与的情况下运行。您自己说过,您希望在单击 span
时更改文本。
// Get a reference to the span
let span = document.getElementById("es_msg");
// Set up a click event that references the correct event handling function
span.addEventListener("click", doClick);
// Define the handler function
function doClick(){
// In the DOM handler, the element that caused the event
// is available via the keyword "this". Also, if you are not
// modifying the HTML content of an element, use .textContent
// not .innerHTML
this.textContent = 'Test';
}
<span id="es_msg">
Your subscription was successful! Kindly check your mailbox and confirm your subscription. If you don't see the email within a few minutes, check the spam/junk folder.</span>
关于javascript - 使用 javascript 更改跨度内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52139151/