Placeholder IE9 - Javascript not executed in IE9

2019-08-28 23:05发布

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条回答
淡お忘
2楼-- · 2019-08-28 23:21

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();
查看更多
登录 后发表回答