rename or replace all urls in html file per javascript

偶尔善良 提交于 2019-12-13 04:34:31

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!