所以我对你来说很酷.
我需要扫描我的html文档,因为它正在渲染并替换每一个
href=""
Run Code Online (Sandbox Code Playgroud)
同
onclick="window.location=''"
Run Code Online (Sandbox Code Playgroud)
更重要的是,我需要携带从href到window.location的链接.
例如,如果我有:
href="http://www.google.com.au"
Run Code Online (Sandbox Code Playgroud)
它会变成:
onclick="window.location='http://www.google.com.au'"
Run Code Online (Sandbox Code Playgroud)
我需要它在文档中的每个href上执行此操作
我不知道,但它需要在jQuery/javascript:D
多谢你们.
你可以试试这个:
$('a').each(function() {
var href = $(this).attr('href');
$(this).attr('onclick', "window.location='" + href + "'")
.removeAttr('href');
});
Run Code Online (Sandbox Code Playgroud)
你想用这个来实现什么?有可能有一种更优雅的方式来实现你所追求的目标.
例如,在JS中自己处理事件可能更好.将第三行替换为:
$(this).click(function() { window.location = href; })
Run Code Online (Sandbox Code Playgroud)
这可能会变得非常昂贵,所以你可能想要考虑jQuery的delegate事件:http://api.jquery.com/delegate/