[英]what is the correct way to use onclick in this part of javascript?
[英]What is the correct way to use the javascript .onclick function?
當使用 .onclick 運行一個函數時,它並沒有執行它。 我做錯了什么嗎?
<script>
const object1 = document.getElementById('object1');
const image2Path = "image2.svg";
object1.onclick = () => {
object1.src = image2Path;
}
</script>
<body>
<img id='object1' src="image1.svg">
</body>
當我單擊對象時,我期待 image1(成功顯示)更改為 image2。
您需要正確綁定 onclick 處理程序。 這將是一種方法:
<script>
function toogle()
{
const object1 = document.getElementById('object1');
const image2Path = "image2.svg";
object1.src = image2Path;
}
</script>
<body>
<img id='object1' src="image1.svg" onclick="toggle();">
</body>
const object1 = document.getElementById('object1'); const image2Path = "image2.svg"; object1.onclick = () => { object1.src = image2Path; }
<body> <img id='object1' src="image1.svg"> </body>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.