如何在占位符属性中设置单个单词的样式

How to style an individual word in Placeholder attribute

有没有办法在占位符属性中设置单个单词的样式?我的营销部门要求我将占位符中的第一个词加粗。下面是想实现的形象营销:

如有任何建议,我们将不胜感激。

样式 <label> 而不是输入

label{
  display: inline-block;
  border: 1px solid #ccc;
  padding: 12px 16px;
}
label input{
  border:none;
  outline:none;
}
<label>
  SEARCH
  <input type="search" placeholder="vendors, healthcare...">
</label>

您不能使用占位符标记来执行此操作,但您可以通过设置元素样式使其以这种方式显示,如以下示例所示:

html

<div><input type="text"/> <span>First</span> <span>name:</span></div>

javascript

$("span").css({"position": "absolute"})

$("span:first").css({"color": "green" })
$("span").eq("1").css({"color": "red", "left": "44px" })

$("input").on("focus", function(){

$("span").hide();

})

$("input").on("blur", function(){
if ($(this).val() == "") { $("span").css("display", "inline-block")}
})

    $("span").click(function(){

    $("span").hide()

    })

css

div {position: relative}
input {color: green}

span {left: 10px}

http://jsfiddle.net/Fgd2e/1/

这个占位符属性是一个 属性 将原始文本作为输入,...它无法设置样式...只能在此处选择它,因此请在您的输入框中使用一些 javascript ..,

看看这个可能会派上用场

function handle(){  
 if(document.getElementById("input").value==""){
   document.getElementById("tag").style='opacity:1';
 }
}

function handle2(){
  if(document.getElementById("input").value!=""){
   document.getElementById("tag").style.display="none"
 }
  else{
    document.getElementById("tag").style.display="inline-block";
  }
}
function handle3(){
  document.getElementById("tag").style.display="none";
  document.getElementById("input").focus();
}

document.getElementById("input").addEventListener('mouseout',handle,false)
document.getElementById("input").addEventListener('keyup',handle2,false)
document.getElementById("tag").addEventListener('click',handle3,false)
input_container{
  position:relative;
}
#input {
  position:relative;
  width:400px;
  height:20px;
  background:red;
  padding:0;
  margin:none;
}
#tag{
  display:inline-block;
 position:absolute;
  border-radius:4px;
  left:2%;
}
<div id="input_container">
  <input id="input">
  <div id="tag"><span style="font-weight:bold">Search</span> vendors,healthcare..</div>
</div>

我觉得这样比较好

var state = true;

function hideText() {
  var info = document.querySelector('#search').value;
  if (state) {
    document.querySelector('#text').style.visibility = "hidden";
    state = false
  } else if (!state && info == "") {
    document.querySelector('#text').style.visibility = "visible";
    state = true;
  }
}

function clickText() {
  document.querySelector('#search').focus()
}

document.querySelector('#search').addEventListener('focus', hideText);
document.querySelector('#search').addEventListener('blur', hideText);
document.querySelector('#text').addEventListener('click', clickText);
:root {
  font-size: 10px;
  --color-main: rgb(66, 157, 199);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
  font-size: 2rem;
}

#container {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

#wrapper {
  position: relative;
}

#text {
  position: absolute;
  top: 1rem;
  left: 1rem;
}

#search {
  width: 50rem;
  height: 5rem;
  padding-left: 1rem;
  border: 3px solid var(--color-main);
  border-radius: 5px;
  font-size: inherit;
  font-family: inherit;
}
<div id="container">
  <div id="wrapper">
    <input id="search" name="busqueda" type="text">
    <label for="busqueda" id="text"> <strong>Hello</strong> World!</label>
  </div>
</div>