Placeholder IE9 - Javascript not executed in IE9

孤街浪徒 提交于 2020-01-06 19:27:36

问题


I'm currently developping a website in Drupal and i used a Javascript to replace the placeholder property in IE8-9. Here's the code :

$('input[placeholder]').focus(function() {
  var input = $(this);
  if (input.val() == input.attr('placeholder')) {
    input.val('');
    input.removeClass('placeholder');
  }
}).blur(function() {
  var input = $(this);
  if (input.val() == '' || input.val() == input.attr('placeholder')) {
    input.addClass('placeholder');
    input.val(input.attr('placeholder'));
  }
}).blur();

But it doesn't seem to be executed. The navigator doesn't go inside the function. When i launch it trough the console it works fine. Does anyone have an idea of how to fix it ?

EDIT : Even when putting the right selector, it's still not working Thanks a lot


回答1:


The placeholder is an attribute of some html elements (inputs), you have to add a selector matching the given attribute:

$('*[placeholder]').focus(function() { //Or input[placeholder]
  var input = $(this);
  if (input.val() == input.attr('placeholder')) {
    input.val('');
    input.removeClass('placeholder');
  }
}).blur(function() {
  var input = $(this);
  if (input.val() == '' || input.val() == input.attr('placeholder')) {
    input.addClass('placeholder');
    input.val(input.attr('placeholder'));
  }
}).blur();


来源:https://stackoverflow.com/questions/33103746/placeholder-ie9-javascript-not-executed-in-ie9

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!