使用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.
更多推荐
发布评论