LANG SELRCT

Apps Script Reference  (Create: Create new Spreadsheet | Create new Apps Script

Sunday, March 11, 2018

JavaScriptで前後の要素を取得する


前後の要素を取得する場合のコードの例

  • nextElementSibling
  • previousElementSibling




コード.gs
function doGet() {
  return HtmlService.createHtmlOutputFromFile("index");
}
意訳
この機能がやること
指定したHTMLファイルを表示する




index.html
<!DOCTYPE html>
<html>
  <body>
    <p>ひとつめ</p>
    <p id="second">ふたつめ</p>
    <p>みっつめ</p>
  </body>
  <script>
    var second = document.getElementById("second");
    var first = second.previousElementSibling.textContent;
    var third = second.nextElementSibling.textContent;
    alert([first, third])
  </script>
</html>
意訳
  


ひとつめのp要素
ふたつめのp要素
みっつめのp要素


idがsecondの要素を取得
secondの前の要素のテキストを取得
secondの後の要素のテキストを取得
アラートに出す




Latest post

Extracting data from Google Sheets with regular expressions

Introduction Regular expressions are a powerful tool that can be used to extract data from text.  In Google Sheets, regular expressions ca...