如何使用Javascript將圖像與URL連結?
P粉652495194
P粉652495194 2024-04-03 10:34:22
0
1
322

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

我該怎麼做?

我試過了,但沒用。

程式碼(I took this from Stack Overflow)

P粉652495194
P粉652495194

全部回覆(1)
P粉638343995

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

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

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!