在plain JS(非jQuery)中删除焦点上的占位符属性

时间:2018-01-19 16:21:52

标签: javascript dom

我只想删除包含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" />

2 个答案:

答案 0 :(得分:2)

要删除属性,您可以使用element.removeAttribute("placeholder");

答案 1 :(得分:1)

您可以使用focus&amp; blur事件

&#13;
&#13;
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;
&#13;
&#13;