Create drop down control containing page's named anchors
I have a dynamically created page (read from database values) with grouped information. I wanted to add named anchors as each group is created. (Regular anchors will appear on the page also.)
I was hoping to find a jQuery plug or codeset example that could automatically generate and populate a drop开发者_如何学编程 down control for the page that provides the navigation to the named anchors.
Thanks for any pointers or examples.
This should do the trick:
(function($){
$.fn.anchorsToSelect = function(selector) {
selector = selector || 'a[name]';
this.html($(selector).map(function(){
return '<option value="#' + this.name + '">' + $(this).text();
}).get().join('</option>'));
this.bind('change', function(){
location.hash = $(this).val();
});
return this;
};
})(jQuery);
Usage: $('select#MyDropDown').anchorsToSelect();
精彩评论