当我按下a-tag时,我想运行以下内容:
<a id="demo">Hello.</a>
<script>
document.getElementById("demo").onclick = function() {myFunction()};
var url = "http://www.google.com";
var myWindow = window.open(url, "http://www.google.com", "width=700,height=275");
</script>
如何正确地实现它,只在按下a-tag时运行var?我基于以下内容:
<a id="demo">Hello.</a>
<script>
document.getElementById("demo").onclick = function() {myFunction()};
function myFunction() {document.getElementById("demo").innerHTML = "Hi there!";}
</script>
非常感谢!
答案 0 :(得分:1)
您在var
函数中未包含这两个onclick
语句,因此它们一直在运行。
此外,要使<a>
元素更正,它需要href
属性。如果你真的不想在任何地方导航,你根本不需要一个<a>
元素,你可以使用几乎任何其他元素,只需为它分配一个click
事件处理程序完全相同的方式你正在使用<a>
元素。请参阅下面的代码示例:
#demo { cursor:pointer; }
<div id="demo">Hello.</d>
<script>
document.getElementById("demo").onclick = function() {
var url = "http://www.google.com";
var myWindow = window.open(url, "http://www.google.com", "width=700,height=275");
};
</script>
答案 1 :(得分:0)
您刚刚丢失了在点击处理程序中调用的<a id="demo">Hello.</a>
<script>
document.getElementById("demo").onclick = function() {
var url = "http://www.google.com";
var myWindow = window.open(url, "http://www.google.com", "width=700,height=275");
};
</script>
的定义。
您可以直接在onclick中移动它,如下所示:
{{1}}&#13;
答案 2 :(得分:0)
您必须编写如下代码:
<a id="demo">Hello.</a>
<script>
document.getElementById("demo").addEventListener("click", function(){
alert("Hi")
})
</script>
答案 3 :(得分:0)
package main
import (
"fmt"
"sync"
)
func main() {
in := gen()
f1 := factorial(in) // code to be replaced
f2 := factorial(in) // code to be replaced
for n := range merge(f1, f2) { // code to be replaced
fmt.Println(n) // code to be replaced
} // code to be replaced
// What I'd like to use instead of the above...
//var res [2]<-chan int
//res[0] = make(chan int)
//res[0] = factorial(in)
//res[1] = make(chan int)
//res[1] = factorial(in)
// for n := range merge(res...)
// fmt.Println(n)
//}
// This commented code generates the following error
//$ go run main.go
//# command-line-arguments
//.\main.go:20: cannot use res (type [2]<-chan int) as type []<-chan int in argument to merge
}
func gen() <-chan int {
out := make(chan int)
go func() {
for i := 0; i < 100; i++ {
for j := 3; j < 13; j++ {
out <- j
}
}
close(out)
}()
return out
}
func factorial(in <-chan int) <-chan int {
out := make(chan int)
go func() {
for n := range in {
out <- fact(n)
}
close(out)
}()
return out
}
func fact(n int) int {
total := 1
for i := n; i > 0; i-- {
total *= i
}
return total
}
func merge(cs ...<-chan int) <-chan int {
out := make(chan int)
var wg sync.WaitGroup
wg.Add(len(cs))
for _, c := range cs {
go func(ch <-chan int) {
for n := range ch {
out <- n
}
wg.Done()
}(c)
}
go func() {
wg.Wait()
close(out)
}()
return out
}