答案 0 :(得分:2)
我认为这样更好。
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>
答案 1 :(得分:1)
设置<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>
答案 2 :(得分:1)
您不能使用占位符标记执行此操作,但您可以通过样式元素以此方式显示它,如下例所示:
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}
答案 3 :(得分: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)
&#13;
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%;
}
&#13;
<div id="input_container">
<input id="input">
<div id="tag"><span style="font-weight:bold">Search</span> vendors,healthcare..</div>
</div>
&#13;