[英]Would like a button to change color and change text as a user clicks
Currently I have this code, which changes text and the colour well, but I require it to snap back to the original colour (blue) and original text if the user decide to not want the product anymore by pressing the Undo Selection. 当前,我有此代码,可以很好地更改文本和颜色,但是如果用户决定通过按“撤消选择”不再想要该产品,则我需要它恢复到原始颜色(蓝色)和原始文本。 I have included what I may think would work, having a link paragraph that the user clicks to Undo Selection and if it is clicked it will revert to the original state.
我包含了我认为可能会起作用的内容,其中有一个链接段落,用户单击该链接即可撤消选择,如果单击该链接,它将恢复为原始状态。 Any suggestions and reasons to why my code is not working would be greatly appreciated.
我的代码无法正常工作的任何建议和理由,将不胜感激。
function changemyButton()
{
var count = 1;
var elem = document.getElementById("button3");
if (elem.value=="Un-Select Package") elem.value = "Select This Package";
else elem.value = "Package Selected!";
var count = 0;
if (elem.value!=="Un-Select Package") {
button3.style.background = "green";
document.write("<p> Undo Selection </p>");
var count= 1;
}
}
This is the HTML: 这是HTML:
<input type='button' value='Select This Package' id='button3' onclick="changemyButton()">
This is the CSS : 这是CSS:
.button3 {
background:#003e7e;
text-shadow: 0px 1px 0px rgba(0, 0, 0, 0.3);
font-size: 12px;
height: 38px;
width: 220px;
margin: 50px 0 0 50px;
overflow: hidden;
display: block;
text-align: center;
line-height: 58px;
color: white;
position:relative;
bottom:55px;
left: -20px;
line-height: 1em;
padding: 0px;strong text
}
I will explain here some bad practices that should be avoided in code like in the question: 我将在这里解释一些不良做法,应在类似问题的代码中避免使用:
// Bad
changeMyButton()
// Good
activate()
deactivate()
// Bad
changeMyButton() { document.getElementById('btn1').style.color = '#eee'; }
// Good
activate(btn) { btn.style.... }
in that way changing the design, the wording requires updating your code, what happens if you want to support other languages in your app ? 以这种方式更改设计,措辞需要更新您的代码,如果您想在应用程序中支持其他语言,会发生什么? use a css class or another appropriate way to test
使用CSS类或其他合适的方法进行测试
// in your method
function onSelect(btn){
var isActive = btn.hasClass('active');
return isActive ? deactivate(btn) : activate(btn);
}
function onSelect(btn) {
var isActive = btn.hasClass('active');
return isActive ? deactivate(btn) : activate(btn);
}
function activate(btn) {
btn.addClass('active');
btn.value = 'Selected';
}
function deactivate(btn) {
btn.removeClass('active');
btn.value = 'Select';
}
You manipulate element style declaration button3.style.background = "green";
您可以操作元素样式声明
button3.style.background = "green";
which has higher specificity than any id or class declaration. 它比任何id或class声明都具有更高的特异性。
And change .
和改变
.
to use #
in CSS selector: 在CSS选择器中使用
#
:
.button3 {
... .button3 {
...
}
It should be #button3
应该是
#button3
Your biggest problem is document.write("<p> Undo Selection </p>");
您最大的问题是
document.write("<p> Undo Selection </p>");
=> this will clear your page. =>这样会清除您的页面。 Second problem is incorrect code =>
button4
was not inited, so nothing done, when clicked. 第二个问题是不正确的代码=>
button4
没有inited,所以没有这样做,点击后。 count
variable does nothing. count
变量什么也不做。 And your css
button4
not used here. 而且您的
css
button4
不在这里使用。 + read answer from @Anima-t3d +阅读@ Anima-t3d的答案
function changemyButton() { var elem = document.getElementById("button3"); if (elem.value == "Un-Select Package") elem.value = "Select This Package"; else if( elem.value == "Package Selected!" ) { elem.value = "Select This Package"; elem.style.background = "red"; } else { elem.value = "Package Selected!"; elem.style.background = "green"; } }
.button4 { background:#003e7e; text-shadow: 0px 1px 0px rgba(0, 0, 0, 0.3); font-size: 12px; height: 38px; width: 220px; margin: 50px 0 0 50px; overflow: hidden; display: block; text-align: center; line-height: 58px; color: white; position:relative; bottom:55px; left: -20px; line-height: 1em; padding: 0px;strong text }
<input type='button' value='Select This Package' id='button3' onclick="changemyButton()">
I have created this with the help of jQuery. 我是在jQuery的帮助下创建的。 So if you choose this solution then you require jquery to get the expected results.
因此,如果选择此解决方案,则需要jquery才能获得预期的结果。
https://jsfiddle.net/d7dg8vsn/ https://jsfiddle.net/d7dg8vsn/
$("#button").click(function(){
$(this).val('Package Selected!');
$(this).css({background: 'blue'});
$('.undo').show();
});
$('.undo').click(function(){
$('#button').val('Select This Package');
$('#button').css({background: 'green'});
$(this).hide();
});
\\ \\
<input type='button' value='Select This Package' id='button' onclick="changemyButton()">
<a href="#" class="undo">
undo
</a>
I think you should use jquery instead of lengthy javascript code.
jQuery will reduce your code also
https://jsfiddle.net/PunitSoniME/tqbyfxaL/1/ https://jsfiddle.net/PunitSoniME/tqbyfxaL/1/
Check above fiddle link
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.