簡體   English   中英

如何使用 javascript 將圖像制作為指向另一個 html 頁面的可點擊鏈接?

[英]How to make an image as a clickable link to another html page using javascript?

這是我寫的 JavaScript 代碼

var goToSecondPage= document.querySelector('.mobile');

goToSecondPage.addEventListener("click", function() {

document.location.href='productDetails.html';
 })

我正在嘗試在索引 html 頁面(稱為移動)上使用現有 class 中的圖像,並將其作為可點擊鏈接圖像指向另一個名為productDetails.html的文件

這取決於您的productDetails.html文件所在的位置。 如果它在根目錄中,請嘗試

var goToSecondPage = document.querySelector('.mobile');

goToSecondPage.addEventListener("click", function() {
    document.location.pathname = '/productDetails.html';
});

這只會改變你的路徑,而不是整個 url。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM