#なにができないか
-
Goを用いてWebページを出力して、そのhtmlページでページ遷移するとPage not foundとかでてエラーとなります
-
htmlにjqueryを使ってみようと試みてもうまくできませんでした
[ディレクトリ構造]
./ -templates -test.html -sample.ls -aaa.html -main.go
go
1main.go 2 3package main 4 5import ( 6 "html/template" 7 "log" 8 "net/http" 9 "path/filepath" 10 "sync" 11) 12 13type templateHandler struct { 14 once sync.Once 15 filename string 16 templ *template.Template 17} 18 19//ServeHTTPはHTTPリクエストを処理する 20func (t *templateHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { 21 t.once.Do(func() { 22 t.templ = 23 template.Must(template.ParseFiles(filepath.Join("templates", t.filename))) //htmlのあるフォルダ指定 24 }) 25 t.templ.Execute(w, nil) 26} 27func main() { 28 http.Handle("/", &templateHandler{filename: "test.html"}) 29 30 if err := http.ListenAndServe(":8080", nil); err != nil { 31 log.Fatal("ListenAndServe:", err) 32 } 33}
html
1 2test.html 3 4<!DOCTYPE html> 5<html> 6 <head> 7 <title>test</title> 8 <meta charset="utf-8"> 9 <script type="text/javascript" src="sample.js"></script> 10 <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script> 11 </head> 12 <body> 13 <button class="a">他のページへ</button> 14 <a href="aaa.html">a</a> 15 </body> 16</html> 17 18a.html 19 20<!DOCTYPE html> 21<html> 22 <head> 23 <title>aaa</title> 24 </head> 25 <body> 26 <p>hello</p> 27 </body> 28</html> 29
js
1$('button').click(function() { 2 3 location.href = './aaa.html'; 4 5})
##疲弊しまくっているせいで変なミスかもしれませんが助けてもらえると幸いです
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/03/08 09:57
2020/03/08 09:58
2020/03/09 01:31 編集