简体   繁体   English

想要一个按钮来更改颜色并在用户单击时更改文本

[英]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

}

NOTE: THIS IS NOT AN ANSWER TO THE QUESTION 注意:这不是问题的答案

I will explain here some bad practices that should be avoided in code like in the question: 我将在这里解释一些不良做法,应在类似问题的代码中避免使用:

Your function should do one thing and do it well 您的功能应该做一件事并且做好

// Bad
changeMyButton()

// Good
activate()
deactivate()

Avoid global variable as much as possible and use arguments instead 尽可能避免使用全局变量,而应使用参数

// Bad
changeMyButton() {  document.getElementById('btn1').style.color = '#eee';  }

// Good
activate(btn) {  btn.style....  }

Do not test the button state based on the label value 不要根据标签值测试按钮状态

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);
} 

Use CSS to change style instead of JavaScript manipulation 使用CSS更改样式而不是JavaScript操作

So a possible program will be like this 所以可能的程序会像这样

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.

相关问题 用户单击时更改输入的背景颜色 - Change background color of an input when user clicks 当用户点击它时更改 div 的颜色 - Change the color of div when user clicks over it 以颜色为输入,并反复更改背景和前景色,直到用户单击“取消”按钮为止 - Take color as input and change the background and foreground color repeatedly till user clicks cancel button 想要在用户单击时像按钮一样将按钮更改为默认状态,反之亦然 - want to change like button to default state when user clicks unlike button and vice versa 当用户单击按钮时,我希望我的ID晃动 - I would like my id to shake when the user clicks a button 用户单击后使按钮像变颜色 - Making like button change color after user click it 有没有一种好的方法可以让当有人点击一个按钮时,比如 1-5 的选项 3,它会改变按钮 1-3 的颜色? - Is there a good way to have it so that when someone clicks a button, say option 3 on a scale of 1-5, that it would change the color of buttons 1-3? 更改文字并在javascript中单击按钮后替换按钮/文本? - Change text and replace button/text upon button clicks in javascript? 使用 makeStyles 更改按钮文本的颜色 - change color of button text with makeStyles 更改ExtJS中的按钮文本颜色 - change button text color in ExtJS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM