简体   繁体   English

"在悬停时更改按钮文本"

[英]Changing button text on hover

I'm trying to change the text of the button when you hover on it, I found a solution but for some reason it won't work.当您将鼠标悬停在按钮上时,我正在尝试更改按钮的文本,我找到了解决方案,但由于某种原因它不起作用。

What I want to happen is when I hover on it it will turn to green and change the text to Completed!<\/code>我想要发生的是当我将鼠标悬停在它上面时它会变成绿色并将文本更改为Completed!<\/code> . .

Currently the button changes color when I hover on it but the text wont change.目前,当我将鼠标悬停在按钮上时,按钮会改变颜色,但文本不会改变。

Here's my css:这是我的CSS:

 .button { border: 0px solid #004B84; background: red; padding: 3px 21px; -webkit-border-radius: 16px; -moz-border-radius: 16px; border-radius: 16px; color: #ffffff; font-size: 12px; font-family: Questrial; text-decoration: none; vertical-align: middle; letter-spacing: 2px; } .button:hover { border: 0px solid #1292e1; background: green; color: white !important; content: "Completed!" !important; } .button:active { background: #1292e1; color: white; text-decoration: none !important; }<\/code><\/pre>
 <button class="button" type="submit" name="completed" value="">New Request<\/button><\/code><\/pre>

And here's the html for the button:这是按钮的 html:

<button class="button" type="submit" name="completed" value="">New Request<\/button><\/code>

"

You could do this using a psuedo element. 您可以使用psuedo元素执行此操作。

 .button { width: 150px; /* set a width so it doesnt change upon hover */ border: 0px solid #004B84; background: red; padding: 3px 21px; -webkit-border-radius: 16px; -moz-border-radius: 16px; border-radius: 16px; color: #ffffff; font-size: 12px; font-family: Questrial; text-decoration: none; vertical-align: middle; letter-spacing: 2px; } .button:hover span { display:none } .button:hover:before { content:"Completed!"; } .button:hover { background-color: green; } 
 <button class="button"> <span>New request</span> </button> 

@K. @K。 Rogers Try Code Hope This Will Work For You! 罗杰斯尝试代码希望这对您有用!

  /* .button */ .button { display: inline-block; position: relative; border: 0px solid #004B84; background: red; padding: 8px 25px; -webkit-border-radius: 16px; -moz-border-radius: 16px; border-radius: 16px; color: #ffffff; font-size: 12px; font-family: Questrial; text-decoration: none; vertical-align: middle; letter-spacing: 2px; overflow: hidden; } .button:hover { background: green; } .button span { transition: 0.6s; transition-delay: 0.2s; } .button:before, .button:after { content: ''; position: absolute; top: 0.67em; left: 0; width: 100%; text-align: center; opacity: 0; transition: .4s,opacity .6s; } /* :before */ .button:before { content: attr(data-hover); transform: translate(-150%,0); } /* :after */ .button:after { content: attr(data-active); transform: translate(150%,0); } /* Span on :hover and :active */ .button:hover span, .button:active span { opacity: 0; transform: scale(0.3); } /* :before pseudo-element on :hover and :after pseudo-element on :active */ .button:hover:before, .button:active:after { opacity: 1; transform: translate(0,0); transition-delay: .4s; } .button:active:before { transform: translate(-150%,0); transition-delay: 0s; } 
  <button class="button" type="button" data-hover="COMPLETED" data-active="I'M ACTIVE"><span>New Request </span></button> 

CSS (without HTML): CSS(无HTML):

You don't need to touch your HTML (manually adding <span> tags, manually removing HTML element content, etc) for this. 为此,您无需触摸HTML(手动添加<span>标签,手动删除HTML元素内容等)。

Just set the button's text content font-size to 0px then add your own text and font-size to the button using :hover , ::after and ::after:hover . 只需将按钮的文本内容的font-size0px然后使用:hover::after::after:hover将自己的文本和font-size添加到按钮。


Check and run the following Code Snippet for a practical example of using just CSS to edit your button's content: 检查并运行以下代码片段,获取仅使用CSS编辑按钮内容的实际示例:

 .button {font-size: 0px;min-width: 100px;min-height: 22px;} .button::after {font-size: 14px;} .button::after { content: "New Request"; } .button:hover::after { content: "Completed!"; } .button:hover { background-color: green; border: 0px solid #1292e1; color: white !important; } 
 <button class="button" type="submit" name="completed" value="">New Request</button> 

JavaScript: JavaScript:

Just use the textContent() property to change your text on hover ( mouseover ) to Completed and back to New Request when hovered out ( mouseout ). 只需使用textContent()属性将悬停时的文本( mouseover )更改为Completed ,当悬停时将其更改为New Requestmouseout )。

Check and run the following Code Snippet for a practical example of the JavaScript approach above: 检查并运行以下代码片段,获取上述JavaScript方法的实际示例:

 var btn = document.querySelector(".button"); btn.addEventListener("mouseover", function() { this.textContent = "Completed!"; }) btn.addEventListener("mouseout", function() { this.textContent = "New Request"; }) 
 .button { min-width: 100px; min-height: 22px; } .button:hover { background-color: green; border: 0px solid #1292e1; color: white !important; } 
 <button class="button" type="submit" name="completed" value="">New Request</button> 

Here is one more solution wit no change button html-code. 这是不更改按钮html代码的另一种解决方案。 Just using css style of pseudo-element ::after To prevent a change of button width on hover you need to define width property. 仅使用伪元素::after css样式为了防止悬停时按钮宽度的改变,您需要定义width属性。

 .button { border: 0px solid #004B84; min-width: 150px; background: red; padding: 5px 21px; -webkit-border-radius: 16px; -moz-border-radius: 16px; border-radius: 16px; color: #ffffff; font-size: 12px; font-family: Questrial; text-decoration: none; vertical-align: middle; letter-spacing: 2px; } .button:hover { background: green; } .button::after { content: "New request!"; } .button:hover::after { content: "Completed!"; } .button:active { background: #1292e1; } .button:active::after { background: #1292e1; } 
 <button class="button" type="submit" name="completed" value=""></button> 

I'm using this in combination with a button size increase.我将它与按钮大小增加结合使用。 Do I have any option to delay the text display until the button is big enough to fit it?我是否可以选择延迟文本显示,直到按钮足够大以适合它? I'd like to avoid that ugly effect when the text doesn't fit.当文本不合适时,我想避免那种丑陋的效果。

 #emailButton{ background-color: grey; color: black; height: 48px; width: 48px; border: none; border-radius: 48px; transition: all 0.5s linear; } #emailButton:hover{ background-color: blue; border: none; color: white; width: 225px; height: 67px; border-radius: 67px; } #emailButton:hover::before{ content: "Some text here" }
 <button class="align-items-center" id="emailButton"> <svg id="emailIcon" width="20" height="16" viewBox="0 0 20 16" fill="none" xmlns="http://www.w3.org/2000/svg"> <path d="M18 0H2C0.9 0 0.00999999 0.9 0.00999999 2L0 14C0 15.1 0.9 16 2 16H18C19.1 16 20 15.1 20 14V2C20 0.9 19.1 0 18 0ZM18 14H2V4L10 9L18 4V14ZM10 7L2 2H18L10 7Z" fill="currentcolor"/> </svg> </button>

Thank you very much!非常感谢你! Miguel Gisbert米格尔·吉斯伯特

See: 看到:

https://www.w3schools.com/cssref/pr_gen_content.asp https://www.w3schools.com/cssref/pr_gen_content.asp

The content property is used with the ::before and ::after pseudo-elements, to insert generated content. content属性与:: before和:: after伪元素一起使用,以插入生成的内容。

You will need Javascript or more. 您将需要Javascript或更多。

eg 例如

<button class="button" type="submit" name="completed" value="" onmouseover="document.getElementsByName('completed')[0].innerHTML ='Completed!';" onmouseleave="document.getElementsByName('completed')[0].innerHTML ='New Request';">New Request</button>

If you want to change the value of an element on a page, then you need to do it per code - try to do it with JavaScript 如果要更改页面上元素的值,则需要按代码进行操作-尝试使用JavaScript进行操作

Edit: Seems like there is a way: 编辑:似乎有一种方法:

How can I replace text with CSS? 如何用CSS替换文本?

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

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