开发者

Add link to multiple items dynamically with jQuery

I am using the following code to create breadcrumbs from an xml document:

var root = null;

$(document).ready( function(){

    $.get( "/_assets/xml/sitemap.xml", 
        function( data ) {
            root = data;
            var pathname = window.location.pathname;

            var local_url = "*[url=" + pathname + "]";

            var parentEls = $(root)
                .find(local_url)
                .parents();

            var mapped = $(parentEls).map(function () { 
                  return 开发者_开发知识库$(this).attr("name"); 
                    })
                .get()
                .reverse()
                .join(" / ");

            $("#breadcrumb").append("<p>" + mapped + "</p>");


        } );
} );

I have the breadcrumbs displaying perfectly, but now I need to dynamically add tags to each element and assign it the url in the xml doc. Could I do this in the .map() function? Not sure if there are any nifty methods in the API that would help me.

Thanks!


Should be as simple as modifying that map function, just have it return an array containing the name and url of the link, for instance in the return of the map function:

return [$(this).attr("url"),$(this).attr("name")];

And then in your breadcrumb line:

$("#breadcrumb").append("<p><a href=\""+mapped[0]+"\">"+mapped[1]+"</a></p>");
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜