如何在输入文本下面添加div?

Rol*_*oll 1 html javascript jquery

我想开发我的简单自动完成,我对JQuery自动完成插件不感兴趣.

我只想在输入文本下面添加一个潜水,这段代码假设有效,我做错了什么?实现这个目标的方法是什么?

的jsfiddle

如果你不想打开小提琴,这里是代码:

$("#txtSearch").keypress(function (e) {
    if ($("#txtSearch").val().length >= 2) {
        var pos = $("#txtSearch").position();
        $('<div />', {
            'html': "xxxxxxxxx  xx  x"
        }).css({
            top: pos.top,
            left: pos.left,
            width: '300px',
            position: 'absolute',
            'background-color': 'Yellow'
        }).appendTo($("#txtSearch"));
    }
});
Run Code Online (Sandbox Code Playgroud)

Sel*_*gam 6

试试下面的代码,

$("#txtSearch").keypress(function (e) {
   if ($("#txtSearch").val().length >= 2) {
      var pos = $("#txtSearch").position();
      $('<div />')
      .html("xxxxxxxxx  xx  x")
      .css({
         top: pos.top + $("#txtSearch").height()  + 1,
         left: pos.left,
         width: '300px',
         position: 'absolute',
         'background-color': 'Yellow'
      }).insertAfter($("#txtSearch"));
   }
});
Run Code Online (Sandbox Code Playgroud)

演示: http ://jsfiddle.net/uZF5g/1/