简体   繁体   English

如何使按钮在图像的位置可点击?

[英]How can I make the button to be clickable in the place of an image?

I have a button which has an image, but that the button is not clickable in the place of image.我有一个带有图像的按钮,但是该按钮在图像的位置不可点击。 It is clickable only in the outside place of the image (in the image name)只在图片的外面的地方可以点击(在图片名称中)

Below is my tags下面是我的标签

 .btn { border: 0px solid white; background: transparent; color: black; font-size: 12px; border-radius: 20px; }.default:hover { background: #e7e7e7; }
 <button type="button" name="search" class="btn default"> <img src="https://placekitten.com/g/100/100" style="border-radius: 50%;" /> image name here </button>

How can I make that button to be clickable in the place of an image?如何使该按钮可以在图像的位置单击?

It's working perfectly.它运行良好。

 <button type="button" name="search" class="btn default" onclick=console.log("clicked")><img src="http://graph.facebook.com/00000000/picture" style="border-radius: 50%;"/>image name here</button>

Just addpointer-events: none;只需添加pointer-events: none; to the img tag.img标签。

 .btn { border: 0px solid white; background: transparent; color: black; font-size: 12px; border-radius: 20px; }.default:hover { background: #e7e7e7; }.btn img { pointer-events: none }
 <button type="button" name="search" class="btn default"><img src="http://graph.facebook.com/00000000/picture" style="border-radius: 50%;"/>image name here</button>

This works as expected -but I modified your code a little to make it more semantically correct.这可以按预期工作-但我稍微修改了您的代码以使其在语义上更正确。 I put the image inside a figure element and the name inside a figcaption element to give better image structure - then an event listener on the button anda console to check - and it is all fine.我将图像放在figure元素中,将名称放在figcaption元素中以提供更好的图像结构 - 然后在按钮上设置事件侦听器和控制台进行检查 - 一切都很好。

 const button = document.querySelector('button'); button.addEventListener('click', clicked); function clicked(){ console.log('I was clicked'); }
 .btn { border: 0px solid white; background: transparent; color: black; font-size: 12px; border-radius: 20px; }.btn img { border-radius: 50%; }.default:hover { background: #e7e7e7; }.btn:hover, .btn:focus { outline: none }
 <button type="button" name="search" class="btn default"> <figure> <img src="https://i.pinimg.com/originals/3e/6b/cd/3e6bcdc46881f5355163f9783c44a985.jpg" width="50"/> <figcaption> fluffy kitten </figcaption> </figure> </button>

This can be done in three ways这可以通过三种方式完成

  1. Image as input图像作为输入

     <input type="image" name="Name of image button" src="Path of the Image file? border="Specfiy Image Border " alt="text">

2. By using css style 2. 通过使用 css 样式
<button onclick="myFunction()"><img src="your image name here.png"></button>

3. using `onclick` javascript function 3. 使用`onclick` javascript function
 <button onclick="myFunction()"><img src="your image name here.png"></button>

Using Onclick:使用 Onclick:

 function clickedButton(){ alert("I am clicked;"); }
 .btn { border: 0px solid white; background: transparent; color: black; border-radius: 20px; width: 150px; height: 30px; cursor: pointer; outline: none; background: url("https://picsum.photos/150/30") no-repeat scroll 0 0 transparent; }.btn:active{ border: 2px solid #111; }.default:hover { background: #e7e7e7; }
 <button onclick="clickedButton()" type = "button" name = "search" class = "btn default" > <img src="">image name here </button>https://stackoverflow.com/posts/67031752/edit#

Using Add Listener使用添加侦听器

 document.getElementById("button").addEventListener("click", clickedButton); function clickedButton() { alert("I as clicked;"); }
 .btn { border: 0px solid white; background: transparent; color: black; border-radius: 20px; width: 150px; height: 30px; cursor: pointer; outline: none; background: url("https://picsum.photos/150/30") no-repeat scroll 0 0 transparent; }.btn:active { border: 2px solid #111; }.default:hover { background: #e7e7e7; }
 <button id="button" type="button" name="search" class="btn default"> <img src="">image name here </button>

Add with onclick添加 onclick

 .btn { border: 0px solid white; background: transparent; color: black; border-radius: 20px; width: 150px; height: 30px; cursor: pointer; outline: none; background: url("https://picsum.photos/150/30") no-repeat scroll 0 0 transparent; }.btn:active{ border: 2px solid #111; }.default:hover { background: #e7e7e7; }
 <button type = "button" name = "search" class = "btn default" > <img src="">image name here </button>

Something like this should help!像这样的东西应该有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM