Почему бы просто не использовать <button>
- вы можете оформить их так, чтобы они выглядели как <span>
, и воспользоваться всеми встроенными функциями.
Возможно, я пропустил стиль, который может понадобиться определенному браузеру, поскольку он делает это из головы, но вы поняли.
важно - ниже показано Вы, как «сбросить» <button>
- пожалуйста, не используйте это в производстве, не добавляя некоторые различия между кнопкой и окружающим текстом.
html{
font-size: 1em;
}
.like-span{
background: none; /*essential*/
border: none; /*essential*/
padding: 0; /*essential*/
font: inherit; /*important as otherwise the text will look slightly different*/
color: inherit; /*if you want the span the same colour as the rest of the sentence*/
cursor: pointer; /*make sure you add this, but if you really want it to behave like a span you would leave this out*/
outline: inherit; /*I would not recommend this as it removes focus indicator, but is fine as part of a reset if you are adding your own style */
}
.blue{
color: blue;
}
<p>This is a sentence with a <button class="like-span">button</button> that looks just like a span</p>
<p class="blue">This is a sentence with a <button class="like-span">button</button> that looks just like a span</p>