前後の要素を取得する場合のコードの例
- 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の後の要素のテキストを取得 アラートに出す |