我只想删除包含if
条件的属性,然后将其添加回else
。
function onFocusChange () {
if (document.getElementById("input").focus()) {
document.getElementById("input").placeholder = "";
else {
document.getElementById("input").placeholder = "This is Placeholder Text";
}
}
}
加价:
<label for="input">
Question #1:
</label>
<input type="text" id="input" name="Address" required="required" placeholder="This is Placeholder Text" />
答案 0 :(得分:2)
要删除属性,您可以使用element.removeAttribute("placeholder");
答案 1 :(得分:1)
您可以使用focus
&amp; blur
事件
var element = document.getElementById('input');
element.addEventListener('focus', function() {
element.setAttribute('placeholder', '')
})
element.addEventListener('blur', function() {
element.setAttribute('placeholder', 'This is Placeholder Text')
})
&#13;
<label for="input">
Question #1:
</label>
<input type="text" id="input" name="Address" required="required" placeholder="This is Placeholder Text" />
&#13;