LoginSignup
1
0

More than 5 years have passed since last update.

【ただのメモ】htmlのタグ内のテキストを変更する

Last updated at Posted at 2018-01-11

CMSで自動作成されるHTMLをJavaScriptで強制的に書き換えるために書いたコード

①htmlのソース(今回はthタグの数字を書き換える)

html
<!DOCTYPE html>
<html>
<head>
  <title>JavaScriptで強制的にテキストを書き換える</title>
</head>
<body>
<table>
  <tbody>
    <tr>
      <th class="number">2</th>
    </tr>
    <tr>
      <th class="number">3</th>
    </tr>
    <tr>
      <th class="number">4</th>
    </tr>
    <tr>
      <th class="number">5</th>
    </tr>
    <tr>
      <th class="number">6</th>
    </tr>
  </tbody>
</table>
</body>
</html>

②数字を書き換えるためのJavaScript(今回は1引いた値に変更)

javascript
var childObj = document.getElementsByClassName("number");
var childList = Object.keys(childObj).map(function(key){return childObj[key]});
(function(){
  childList.forEach(
    function(value){
      var a = parseInt(value.innerText);
      value.innerText = a - 1;
    }
  );
})();
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0