Apps Scriptリファレンス: Apps Script Reference |障害・課題追跡: IssueTracker |Google Workspace: Status Dashboard - Summary

2018年3月11日日曜日

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

Googleドライブ内の音声ファイルをiframe内で再生したい

iframe の src にGoogleドライブ内の音声ファイルを埋め込む例です (今回試した音声ファイルはmp3) Code.gs function doGet () { return HtmlService . createTemplateFromFile ( ...