jquery href question
I moved all my pdf files into another folder but I don't want to have to change all the link and adding pdf/ 开发者_JAVA百科in the href. is there a faster and simpler way to do this?
i.e.
<a href="test.pdf">test</a>
change to
<a href="pdf/test.pdf">test</a>
Use the <base>
tag:
<html>
<head>
<base href="pdf"/>
</head>
<body>
<a href="test.pdf">test</a>
</body>
</html>
If your document is stored on a *nix system, I recommend using sed
to fix the links:
sed 's/\"\(.*\.pdf\)\"/\"pdf\/\1\"/' index.html > new_index.html
It is theoretically possible with jQuery, but you're much better off fixing the actual links, so users with JavaScript turned off, bots etc. can access the content and don't run into a 404.
I agree that you should fix the links to work correctly without JavaScript, but as a though experiment this is how you could do it in jQuery:
$('a[href$=".pdf"]').not('a[href^="http"]').attr('href', function(i, val) { return 'pdf/' + val });
DEMO: http://jsfiddle.net/marcuswhybrow/XRfZP/
thanks to @Sime
精彩评论