2017-04-12 58 views
0

如何将占位符的颜色更改为白色?我知道我必须使用-moz占位符,但我不知道如何将其格式化为JAVASCRIPT。我需要在javascript的格式中匹配下面的代码。如何更改javascript格式占位符的颜色?

  //User Name Input 
      var inputUserName = document.createElement("input"); 
      inputUserName.type = "text"; 
      inputUserName.style.bottom = "220px"; 
      inputUserName.style.width = "170px"; 
      inputUserName.style.height = "20px"; 
      inputUserName.style.left = "50px"; 
      inputUserName.style.textAlign = "center"; 
      inputUserName.style.display = "none"; 
      inputUserName.style.backgroundColor = "transparent"; 
      inputUserName.style.borderBottom = "2px solid black"; 
      inputUserName.style.borderTop = "transparent"; 
      inputUserName.style.borderLeft = "transparent"; 
      inputUserName.style.borderRight = "transparent"; 
      inputUserName.placeholder = "User Name"; 
      inputUserName.style.color = "white"; 
      inputUserName.style.position = "absolute"; 
      inputUserName.className = "UserNameSignUp"; 
      inputUserName.UserNameSignUp = "-moz-placeholder"; 
     //input.className = "css-class-name"; // set the CSS class 
     formArea.appendChild(inputUserName); // put it into the DOM 

回答

0

-moz-占位是一个伪类,因此不能直接DOM的一部分,所以你不能编辑它以同样的方式或添加内嵌样式它。

一种解决方法是改变样式表动态像

document.styleSheets[0].insertRule('#xyz:-moz-placeholder { background-color: white; }', 0); 

考虑到它有一定的ID( “#XYZ”)。

请参阅此文档以供参考: https://developer.mozilla.org/en-US/docs/Web/API/CSSStyleSheet/insertRule