简体   繁体   English

jQuery元素可用于append(),但不能用于after()

[英]jQuery element works with append() but not after()

I am modifying a script to display a warning on password fields if CAPS lock is on. 我正在修改脚本以在CAPS锁定处于打开状态时在密码字段上显示警告。

The element that I am building is added to the page just fine if I use the append() method but that's not very extensible as it would rely on the password field having a relevant parent element. 如果我使用append()方法,则将要构建的元素很好地添加到页面中,但这不是很可扩展,因为它将依赖于具有相关父元素的password字段。 What I want to do is add it after() the password field but when I do it is displayed as [object Object] . 我想要做的是在after()密码字段中添加它after()但是当我将其显示为[object Object]

Here's what I have so far: 这是我到目前为止的内容:

$(document).ready(function(){
  $(":password").bind("keypress", function(e) {
    el = jQuery('<div/>', {
      id: 'caps_warning',
      text: 'CAPS lock is on'
    })

    kc = e.keyCode ? e.keyCode : e.which;
    sk = e.shiftKey ? e.shiftKey : ((kc == 16) ? true : false);

    if(((kc >= 65 && kc <= 90) && !sk) || ((kc >= 97 && kc <= 122) && sk)) {
      el.appendTo(e.currentTarget.parentElement);
    } else {
    }
  });
});

I want to replace el.appendTo(e.currentTarget.parentElement); 我想替换el.appendTo(e.currentTarget.parentElement); with e.currentTarget.after(el); e.currentTarget.after(el); .

e.currentTarget is a dom node, not a jQuery object. e.currentTarget是一个dom节点,而不是jQuery对象。 It is also the same as this 也跟this

Try 尝试

$(this).after(el)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM