3 回答

TA貢獻(xiàn)1796條經(jīng)驗(yàn) 獲得超7個(gè)贊
我不確定我是否理解對(duì)了。但我認(rèn)為你在這里不需要這么復(fù)雜的東西,你可以簡(jiǎn)單地使用placeholder
. 然后您可以使用事件在輸入焦點(diǎn)上清除它,如果您愿意onfocus
,可以再次使用事件顯示它。onblur
<input type="text" placeholder="this is value" onfocus="this.placeholder = ''" onblur="this.placeholder = 'this is value'" id="text">
否則,您可以簡(jiǎn)單地創(chuàng)建占位符并保留它,直到用戶用他們想要的值填充輸入。
<input type="text" placeholder="this is value" id="text">

TA貢獻(xiàn)1744條經(jīng)驗(yàn) 獲得超4個(gè)贊
我不完全知道你在嘗試什么,但我認(rèn)為你可能需要一個(gè)blur活動(dòng)。
var input = document.getElementById("text");
var value = "on";
input.addEventListener("focus", function() {
if (value == "on") {
input.value = "";
value = "off";
}
else {
text.value = "this is value";
value = "on";
}
});
input.addEventListener("blur", function () {
input.value = "this is value";
value = "on";
});
<input type="text" value="this is value" id="text">

TA貢獻(xiàn)1801條經(jīng)驗(yàn) 獲得超16個(gè)贊
事件旁邊onfocus是onblur事件。第二個(gè)將被調(diào)用,當(dāng)其他東西被喜歡并且輸入失去焦點(diǎn)時(shí)。查看這篇文章。
以下代碼應(yīng)滿足您的要求:
document.getElementById("text").onfocus = function() {
document.getElementById("text").value=""
}
document.getElementById("text").onblur = function() {
document.getElementById("text").value="this is value"
}
添加回答
舉報(bào)