使用UI自动完成添加滚动到Div功能(Adding Scroll to Div Function With UI Auto Complete)

编程入门 行业动态 更新时间:2024-10-14 22:16:35
使用UI自动完成添加滚动到Div功能(Adding Scroll to Div Function With UI Auto Complete)

你能看一下这个演示 ,让我知道如何从自动完成滚动到所选项目的div?

$(document).ready(function () { $('#tags').on('change', function () { $('#tagsname').html('You selected: ' + this.value); $('html,body').animate({ scrollTop: $("#"+ this.value).offset().top }); }).change(); $('#tags').on('autocompleteselect', function (e, ui) { $('#tagsname').html('You selected: ' + ui.item.value); }); });

但是我收到了这个错误

未捕获的TypeError:无法读取未定义的属性“top”

Can you please take a look at This Demo and let me know how I can enable scroll to div of selected item from auto complete?

$(document).ready(function () { $('#tags').on('change', function () { $('#tagsname').html('You selected: ' + this.value); $('html,body').animate({ scrollTop: $("#"+ this.value).offset().top }); }).change(); $('#tags').on('autocompleteselect', function (e, ui) { $('#tagsname').html('You selected: ' + ui.item.value); }); });

but I am getting this error

Uncaught TypeError: Cannot read property 'top' of undefined

最满意答案

change事件不存在,因此您的代码未被触发。 你必须这样做:

$(document).ready(function () { $('#tags').on('autocompleteselect', function (e, ui) { $('#tagsname').html('You selected: ' + ui.item.value); $('html,body').animate({ scrollTop: $("#"+ this.value).offset().top }); }); });

查看此演示 。

The change event does not exist so your code is not being triggered. you have to do it this way:

$(document).ready(function () { $('#tags').on('autocompleteselect', function (e, ui) { $('#tagsname').html('You selected: ' + ui.item.value); $('html,body').animate({ scrollTop: $("#"+ this.value).offset().top }); }); });

Check this demo.

更多推荐

本文发布于:2023-08-04 00:37:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1405311.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自动完成   功能   Div   UI   Adding

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!