Pregunta

I need a little help with this...I'm not too familiar with jQuery (still learning), and so not sure if this is even possible.

I've basically loaded a section of a page using $("#my-div").load("http://www.demo.com #container")

The contents of #container load just find in #my-div. The problem is, the links loaded are relative and therefor invalid.

Is there as way to alter the content loaded such that the url can be changed from something like this:

<a href="/this/sample-link-to-page">sample link</a>

to

<a href="http://mysite.com/this/sample-link-to-page">sample link</a>

Thank you.

¿Fue útil?

Solución

You should try something like:

$("#my-div a").each( function(index){
    var link = $(this).attr('href');
    link = 'http://mysite.com' + link;
    $(this).attr('href', link);
});

You get all the links, then iterate over changing them.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top