最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - jquery UI AutoComplete - embedding the list in another container - Stack Overflow

matteradmin6PV0评论

My app has a pretty unique style for all the popups that occur, and I can't make the generated jQuery UI Autoplete html work to fit into that style. What I'd like to do is place the autoplete into a pre-made container that's configured to drop in.

However, when I use the appendTo option, the list doesn't appear, and it seems to be because the style is positioning it somewhere outside the visible area of the popup.

$('#name').autoplete({
  minLength: 0,
  source:"this,that,these,those".split(','),
  appendTo: '#popover-hook'
});

Everything works fine when appendTo isn't set.

So: is there a way to put the contents of the autoplete into another div that's placed correctly already?

My app has a pretty unique style for all the popups that occur, and I can't make the generated jQuery UI Autoplete html work to fit into that style. What I'd like to do is place the autoplete into a pre-made container that's configured to drop in.

However, when I use the appendTo option, the list doesn't appear, and it seems to be because the style is positioning it somewhere outside the visible area of the popup.

$('#name').autoplete({
  minLength: 0,
  source:"this,that,these,those".split(','),
  appendTo: '#popover-hook'
});

Everything works fine when appendTo isn't set.

So: is there a way to put the contents of the autoplete into another div that's placed correctly already?

Share Improve this question asked May 8, 2011 at 17:56 Tim SullivanTim Sullivan 16.9k12 gold badges75 silver badges120 bronze badges 2
  • What does the markup for the existing div look like? How is it styled? – Andrew Whitaker Commented May 8, 2011 at 18:34
  • can you setup a jsfiddle with the current code? – ariel Commented May 9, 2011 at 1:41
Add a ment  | 

1 Answer 1

Reset to default 4

The autoplete widget creates a UL element containing the suggestions. The appendTo option specifies where that UL element gets inserted into the DOM. By default, the UL element is appended to the BODY. Styles are then used to position the UL in the correct location.

The autoplete dropdown adds the ui-autoplete and ui-menu-item classes. You should be able to apply your style to those classes. If not, can you give us an essence of the unique styles that?

For positioning, you can override the styling of the UL after the open event fires. Here's an example of absolute positioning:

$('#name').autoplete({
    minLength: 0,
    source:"this,that,these,those".split(','),
    open: function(event, ui) {
        $(".ui-autoplete").css("position", "absolute");
        $(".ui-autoplete").css("top", "100px");
        $(".ui-autoplete").css("left", "100px");
    }
});

Here is an example of positioning inside a container element:

$('#name').autoplete({
    minLength: 0,
    source:"this,that,these,those".split(','),
    appendTo: "#container",
    open: function(event, ui) {
        $(".ui-autoplete").css("position", "relative");
        $(".ui-autoplete").css("top", "0px");
        $(".ui-autoplete").css("left", "0px");
    }
});
Post a comment

comment list (0)

  1. No comments so far