【问题标题】:Clear a paragraph using document.getElementById in HTML在 HTML 中使用 document.getElementById 清除段落
【发布时间】:2021-04-30 23:24:54
【问题描述】:

我正在用 HTML 制作一个计算器。我想使用清除按钮清除结果。但这件事不起作用。

这是清除按钮的 HTML 代码:

<button onclick="clear()">C</button>

这是我的这个函数的 Javascript 代码:

function clear()
{
    document.getElementById('text').innerHTML=" ";//text is the ID of the paragraph tag which holds the string which the user has passed
    document.getElementById('answer').innerHTML=" ";//answer is the ID of the paragraph tag which shows the answer
}

请帮帮我。提前致谢!!

【问题讨论】:

  • 您发布的代码看起来不错。请分享更多上下文
  • ID 匹配吗?看不到任何错误
  • 感谢大家的建议和关注,但我知道这件事与我的休息代码一起工作。 mpm给出的答案帮我解决了问题。

标签: javascript


【解决方案1】:

正如这个问题中提到的(How do I clear inner HTML) 你正在覆盖document.clear,这可能会导致错误。

考虑为您的函数选择另一个名称。

【讨论】:

  • @AgastyaGarg 如果它正在工作,那么请接受答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-12
  • 1970-01-01
  • 2014-07-29
相关资源
最近更新 更多