简体   繁体   English

如何使用CSS更改文本字段中初始文本的颜色?

[英]How do you change the color of the initial text in a text field using CSS?

当你点击该字段时,如何使文本消失,当你点击它时再次出现?

The color css property is used to set the text color. color css属性用于设置文本颜色。 You can use a name value, rgb value, or hex value. 您可以使用名称值,rgb值或十六进制值。

Changing the visibility of elements in reaction to events will required some javascript knowledge. 改变元素对事件的反应可见性需要一些javascript知识。 You can use jQuery, which is a javascript library, to accomplish this. 您可以使用jQuery(一个javascript库)来完成此任务。 By toggling behaviors, you could make an element disappear and then reappear. 通过切换行为,您可以使元素消失然后重新出现。

If you're wanting something like having default text in the textbox until the user focuses on that, you'll need to handle the focus and blur events. 如果您想要在文本框中使用默认文本,直到用户关注它,您将需要处理焦点和模糊事件。 This posting has a tutorial on this. 这篇文章有一个关于此的教程。

do you just mean the color attribute for your first question? 你的意思是第一个问题的颜色属性吗? As for making the text disappear when you click into the field, do you mean an html input field? 至于单击字段时文本消失,你的意思是html输入字段吗?

If it is just a div, then setting an onmouseover and onmouseout event to hide the div (maybe use display:block and display:none) can accomplish that. 如果它只是一个div,那么设置onmouseover和onmouseout事件来隐藏div(也许使用display:block和display:none)可以实现这一点。

CSS: CSS:

input.placeholder {
    color:#ccc;
}

JavaScript: JavaScript的:

(function() {
    var placeholders = document.getElementsByClassName('placeholder');
    for(var p = 0; p < placeholders.length; p++) {
       var placeholder = placeholders[p];
       placeholder.onfocus = function() {
          this.value = '';
          this.removeClass('placeholder');
       };
       placeholder.onblur = function() {
          if(this.value == '') {
             this.addClass('placeholder');
          }
       }
    }
})();

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

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