javascript - 使用 javascript 更改跨度内容

标签 javascript jquery html

它的工作方式是这样的:当我单击订阅按钮时,它会显示一条消息:

<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/

相关文章:

javascript - 水平滚动到容器内的下一个元素会阻止标准滚动

php - 如何使用密码保护网站上可下载的 pdf 文件

javascript - 使用 javascript 和按钮 onclick 生成新表单

javascript - 如何动态获取html5中的图表和报告?

javascript - Angular js像重叠的电子邮件线程一样显示gmail

c# - 在计时器上刷新 Gridview 但保持扩展 div 的状态

javascript - jQuery——无法更改用 AJAX 填充的选择框的值

php - 从未知路径读取 CSV 文件,php

javascript - scrollTop 在 firefox 和 IE 中不起作用

javascript - 按键时复选框状态发生变化