我有一个图像,我想链接它与url.我该怎么做呢?我正在使用Javascript



我有一个图像,我想链接它与url。我用过javascript。代码运行代码之后我想要显示这张图片,当我点击它时,我想要打开一些其他的页面,这些页面已经链接到这个图片,并带有url。

我该怎么做呢?

我试过了,但是行不通。

Code(from Stack Overflow)

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)
}
}
<h3> Image will show here </h3>
<div id="image-container"></div>
<button onclick="handleClick()">Show  me </button>

我希望这个简单的片段对你有帮助。

相关内容

最新更新