问题
I have a jquerymobile web app and want to keep all existing hundreds of different external url links in the html file like
<a href="http://www.example.com" target="_blank" data-rel="external">Link</a>
but want to make them behave like this:
<a href="javascript:intel.xdk.device.launchExternal('http://www.example.com');">
How would I do that (without search and replace) with a script? Thanks a lot for your help.
回答1:
I suppose you want to add an event handler for all links, like this:
$(document).on('click', 'a', function() {
this.href = "javascript:intel.xdk.device.launchExternal('" + this.href + "');";
});
The job will be done only when the link is clicked.
Or, thanks to bencol:
$(document).on('click', 'a', function() {
javascript:intel.xdk.device.launchExternal(this.href);
return false;
});
回答2:
If you can use jquery or jquery mobile, use this to replace all links
$(function() {
$("a").each(function() {
$(this).attr("href", "javascript:intel...('" + $(this).attr("href") +"')");
});
});
来源:https://stackoverflow.com/questions/21832869/rename-or-replace-all-urls-in-html-file-per-javascript