javascript - 在 HTML 中定义常量

标签 javascript html constants

我在 3 个 HTML 页面上有多个表格,这些表格的标题保持不变,即

<thead>
<tr>
 <th>MyHeading</th>
</tr>
</thead>

我想存储 MyHeading字符串在常量中,并在我所有表的 HTML 页面中使用它。 我只能使用纯 HTML 和 javascript。 我如何在纯 HTML 中执行此操作,即在某些常量中存储静态文本,以便我可以在 HTML 页面中重用它们

最佳答案

在 HTML 中这是不可能的。你要么使用一些模板语言和JavaScript。通过使用 javascript,这就是您的答案

var length = document.getElementsByClassName('head').length;
for(var i=0; i< length; i++) {
  document.getElementsByClassName('head')[i].innerHTML = "Demo Head";
}
<table style="width:100%">
  <tr><th class="head">heading</th></tr>
  <tr>
    <td>Jill</td>
    <td>Smith</td> 
    <td>50</td>
  </tr>
  <tr>
    <td>Eve</td>
    <td>Jackson</td> 
    <td>94</td>
  </tr>
</table>
<table style="width:100%">
  <tr><th class="head">heading</th></tr>
  <tr>
    <td>Jill</td>
    <td>Smith</td> 
    <td>50</td>
  </tr>
  <tr>
    <td>Eve</td>
    <td>Jackson</td> 
    <td>94</td>
  </tr>
</table>

关于javascript - 在 HTML 中定义常量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37807597/

相关文章:

javascript - 想要在不创建新类的情况下在正确的位置显示警告图标

c - 将参数传递给具有 const 参数的函数 : is it faster?

c++ - 我们可以在 constexpr 函数中省略局部变量的 const 吗?

c - 为什么在 C 中 const 对象不是编译时常量表达式?

javascript - Netbeans 额外不需要的代码提示

javascript - 函数在一个参数上有闭包,而另一个参数是从 promise 返回的?

javascript - AJAX更新对象列表

javascript - 显示弹出窗口时 GIF 动画停止

html - 为什么内联元素的顶部边框会溢出?为什么不左边框?

html - 具有水平居中内容的布局的两种背景颜色