将原型脚本转换为jquery

将原型脚本转换为jquery,jquery,prototypejs,Jquery,Prototypejs,我在某个地方的一篇文章中发现了这个函数,它似乎将prototype与jquery脚本混合在一起,但我不想包含prototype,因为它破坏了其他一切。所以我想看看是否有人能帮我把它转换成jQuery 该函数应该检测浏览器是否自动填写表单字段,并相应地将类设置为其标签 $("label.placeholder + .input-text").each(function (type) { Event.observe(window, 'load', function () { setTimeo

我在某个地方的一篇文章中发现了这个函数,它似乎将prototype与jquery脚本混合在一起,但我不想包含prototype,因为它破坏了其他一切。所以我想看看是否有人能帮我把它转换成jQuery

该函数应该检测浏览器是否自动填写表单字段,并相应地将类设置为其标签

$("label.placeholder + .input-text").each(function (type) {
Event.observe(window, 'load', function () {
    setTimeout(function(){
        if (!input.value.empty()) {
            input.previous().addClassName('has-text');
        }
    }, 200);
});
});
问题是我一直收到错误
事件。观察不是一个函数


非常感谢您的帮助

我认为这可能会有所帮助:

$(function() {
  setTimeout(function(){
    $("label.placeholder + .input-text").each(function() {
      if ($(this).val()) $(this).prev().addClass('has-text');
    });
  }, 200);
});

我认为这可能有帮助:

$(function() {
  setTimeout(function(){
    $("label.placeholder + .input-text").each(function() {
      if ($(this).val()) $(this).prev().addClass('has-text');
    });
  }, 200);
});

如果您所做的是在加载时检查缓存值,那么您可以尝试以下方法:

$(window).load(function(e){

setTimeout(function(t){
  $('label.placeholder + .input-text').each(function(index, element){
    if (!$(element).val())
    {
      $(element).prev().addClass('has-text');
    }
  });
}, 200);

});
它应该删除对setTimeout的过多调用


您可以将第一行更改为:
jQuery(函数($){
,但我不确定自动填写的表单是在
document.onready
事件还是
window.onload
事件之后加载的。您只需玩一下它就可以知道了。

如果您正在做的是检查onload中的缓存值,那么您可以尝试以下操作:

$(window).load(function(e){

setTimeout(function(t){
  $('label.placeholder + .input-text').each(function(index, element){
    if (!$(element).val())
    {
      $(element).prev().addClass('has-text');
    }
  });
}, 200);

});
它应该删除对setTimeout的过多调用


您可以将第一行更改为:
jQuery(函数($){
,但我不确定自动填写的表单是在
文档.onready
事件还是
窗口.onload
事件之后加载的。您只需玩一下它就可以知道了。

看起来我找到了…可能不是最好的方法,但它很有效!这是我的新代码:

$("label.placeholder + .input-text").each(function (type) {

  // Check if anything is filled in on page load
  if( $(this).val() !== '') {
    $(this).prev("label.placeholder").addClass("has-text").removeClass("focus");
  } else {
    $(this).prev("label.placeholder").removeClass("has-text").removeClass("focus");
  }

  //Change the label style
  $(this).focus(function () {
    $(this).prev("label.placeholder").addClass("focus");
  });

  //Check if user has typed anything in the field
  $(this).keyup(function () {
    $(this).prev("label.placeholder").addClass("has-text").removeClass("focus");
  });

  //
  $(this).blur(function () {

    // Loops through all fields to see if the browser has auto-filled after the user inputted something
    setTimeout(function(t){
      $('label.placeholder + .input-text').each(function(index, element){
        if( $(element).val() !== '') {
          $(this).prev("label.placeholder").addClass("has-text").removeClass("focus");
        }
      });
    }, 200);

    // If the field is left blank, set the label back to default
    if($(this).val() === "") {
      $(this).prev("label.placeholder").removeClass("has-text").removeClass("focus");
    } else {
      $(this).prev("label.placeholder").addClass("has-text").removeClass("focus");
    }
});
问题是,我是否应该检查所有字段,以查看是否在
$(this.blur)或
$(this.keyup)上有值


再次感谢您的帮助和建议!

看来我已经明白了……这可能不是最好的方法,但它确实有效!以下是我的新代码:

$("label.placeholder + .input-text").each(function (type) {

  // Check if anything is filled in on page load
  if( $(this).val() !== '') {
    $(this).prev("label.placeholder").addClass("has-text").removeClass("focus");
  } else {
    $(this).prev("label.placeholder").removeClass("has-text").removeClass("focus");
  }

  //Change the label style
  $(this).focus(function () {
    $(this).prev("label.placeholder").addClass("focus");
  });

  //Check if user has typed anything in the field
  $(this).keyup(function () {
    $(this).prev("label.placeholder").addClass("has-text").removeClass("focus");
  });

  //
  $(this).blur(function () {

    // Loops through all fields to see if the browser has auto-filled after the user inputted something
    setTimeout(function(t){
      $('label.placeholder + .input-text').each(function(index, element){
        if( $(element).val() !== '') {
          $(this).prev("label.placeholder").addClass("has-text").removeClass("focus");
        }
      });
    }, 200);

    // If the field is left blank, set the label back to default
    if($(this).val() === "") {
      $(this).prev("label.placeholder").removeClass("has-text").removeClass("focus");
    } else {
      $(this).prev("label.placeholder").addClass("has-text").removeClass("focus");
    }
});
问题是,我是否应该检查所有字段,以查看是否在
$(this.blur)或
$(this.keyup)上有值


再次感谢您的帮助和建议!

这不起作用。
输入定义在哪里?为什么每次迭代中都应该有一个
onload
观察者?这就是您从代码中得到的一切吗?这不起作用。
输入定义在哪里?为什么每次迭代中都应该有一个
onload
观察者?这就是您从中得到的一切吗代码?感谢您的输入,我似乎无法让它完全按照我希望的方式工作。这与其说是关于在文档加载时自动填充,不如说是关于用户何时开始填充表单以及他们的浏览器何时自动完成表单的其余部分(例如,开始键入名字,从浏览器的“自动填充”中进行选择,其余字段将被填充)感谢您的输入,我似乎无法让它完全按照我希望的方式工作。这与其说是文档加载时自动填充,不如说是用户开始填充表单时,他们的浏览器自动完成表单的其余部分(例如,开始键入名字,从浏览器的“自动填充”中进行选择,其余字段将被填充)感谢您的输入,我似乎无法让它完全按照我想要的方式工作。请参阅上面的我的回复了解我要做的事情。谢谢您的输入,我似乎无法让它完全按照我想要的方式工作。请参阅上面的我的回复了解我要做的事情