主.go
func thandle(rw http.ResponseWriter, req *http.Request) {
t, _ := template.ParseFiles("basic.html")//
t.Execute(rw, nil)
}
func main() {
http.HandleFunc("/", thandle)
http.ListenAndServe(":9999", nil)
}
我不明白。 哪一部分错了?
基本.html
<head>
<script src="js/jquery-1.10.2.min.js"></script>
<script>
$(document).ready(function() {
alert('hey');
});</script>
</head>
如果可以的话 给我看正确的代码示例?
最佳答案
与 Go 无关的问题。尝试:
<head>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script>
$(document).ready(function() {
alert('hey');
});</script>
</head>
您需要了解客户端和服务器端编程之间的区别。也许这些链接会对您有所帮助:
- https://softwareengineering.stackexchange.com/questions/171203/what-are-the-difference-between-server-side-and-client-side-programming
- http://openmymind.net/2012/5/30/Client-Side-vs-Server-Side-Rendering/
如果您想托管自己的 js 文件(另一个静态文件)以便可以使用真实 url(不需要更改 HTML 代码),请参阅 http://golang.org/pkg/net/http/#example_FileServer_stripPrefix
关于Javascript 在 go 网络服务器中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21364564/