开发者

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();

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜