Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
492 views
in Technique[技术] by (71.8m points)

javascript - 首次点击后Javascript停止工作(Javascript stop working after first click)

i tried to change the direction of the arrow by changing the scr of the image.(我试图通过更改图像的scr来更改箭头的方向。)

"www.bsbbalulstudentilorbucuresteni.ro/index" you can see here the problem.(您可以在这里看到“ www.bsbbalulstudentilorbucuresteni.ro/index”问题。) <img id="hnav" src="img/down-arrow.svg" class="icon-hnav" onclick="myFunction()"> function myFunction() { var x = document.getElementById("myDIV"); if (x.style.display === "none") { x.style.display = "block"; } else { x.style.display = "none"; } var x = document.getElementById("hnav"); if (x.style.bottom === "5px") { x.style.bottom = "50px"; } else { x.style.bottom = "5px"; } var x = document.getElementById("hnav"); if (x.src === "img/up-arrow.svg") { x.src = "img/down-arrow.svg"; } else { x.src = "img/up-arrow.svg"; } }   ask by VlWWv translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The src property of an element will return the full path:(元素的src 属性将返回完整路径:)

img.src = 'foo.png'; console.log(img.src); <img id="img"> So, on further clicks, either check against the full path:(因此,在进一步点击时,请对照完整路径进行检查:) if (x.src === "http://www.bsbbalulstudentilorbucuresteni.ro/img/up-arrow.svg") { or check against the src attribute instead:(或改为检查src属性:) if (x.getAttribute('src') === "img/up-arrow.svg") { img.src = 'foo.png'; console.log(img.getAttribute('src')); <img id="img">

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...