开发者

jQuery Arrary with URL's--Convert to Clickable Links

I have an array of URL's that I'm getting from a CAML query (something 开发者_JAVA百科like: http://www.domain.com/site/files/doc1.pdf,http://www.domain.com/site/files/doc2.pdf). The array can have any number of comma-separated URL's depending on the query. What I need to do, and cannot wrap my head around, is take each one of those URL's, get the filename, then somehow create DOM elements such as <a href=http://www.domain.com/site/files/doc1.pdf>doc1</a>. I don't know if it would need a 2D array or what but this is a bit above my level. The array name is 'filesarray' and I know that you can get the filename from the URL by doing:

var index = filesarray.lastIndexOf("/");
var filename = filesarrary.substr(index);

Apart from that I'm pretty lost. Any help would be greatly appreciated.


var arr = [
    'http://www.domain.com/site/files/doc1.pdf',
    'http://www.domain.com/site/files/doc2.pdf',
]
for (i in arr) {    
    var href = arr[i];
    var text = arr[i].split('/').pop();
    $('body').wrap('<a href="'+href +'">'+text+'</a>'); //or append/prepend/html istead wrap
}


try this:

<div id="linksHolder"></div>

    <script type="text/javascript">
        var stringOfLinks = "http://something.com/something.pdf, http://something1.com/somethingElse.doc";
        var arrayOfLinks = stingOfLinks.split(",");
        var fileName = '';
        var index = '';
        for(var i=0; i<arrayOfLinks.length; i++) {
            index = arrayOfLinks[i].lastIndexOf("/");
            fileName = arrayOfLinks[i].substr(index);
            $("#linksHolder").add('<a href="'+arrayOfLinks[i]+'">'+fileName+'</a>')
        }
    </script>


How about something like this?

 var urlArray = listOfUrls.split(",");
 var i;
 var index = 0;
 var filename = "";
 $(urlArray).each(function(i, url){
       index = filesarray.lastIndexOf("/");
       filename = filesarrary.substr(index);

       var elem = $('<a href="'+url+'">'+filename+'</a>');
       //do whatever you want with the dom element,like:
       $("body").append(elem);
  });

I'm assuming that listOfUrls holds a comma separated list of urls (as you stated).

Let me know if it works the way you want it, or I can modify it a bit.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜