首頁  >  問答  >  主體

如何使用Javascript將圖像與URL連結?

我有一張圖片,我想將其與網址連結。我用過javascript。 程式碼 after running the 程式碼 我想顯示此圖像,當我單擊它時,我想打開其他一些頁面,該頁面已通過 url 連結到此圖像。

我該怎麼做?

我試過了,但沒用。

程式碼(I took this from Stack Overflow)

P粉652495194P粉652495194180 天前329

全部回覆(1)我來回復

  • P粉638343995

    P粉6383439952024-04-04 00:26:46

    function handleClick(){
     const image = new Image ()
    
    image.src = "https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png"
    image.style.width="200px"
    image.style.height="200px"
    image.alt="Google logo"
    image.addEventListener("click", function() {
      window.location.href="https://www.google.com"
    })
    
    const element = document.getElementById('image-container')
    if(element){
       element.appendChild(image)
    }
    
    }

    Image will show here

    我希望這個簡單的程式碼片段能夠幫助您。

    回覆
    0
  • 取消回覆