所以我有一个很酷的给你。
我需要扫描我的 html 文档,因为它正在呈现并替换每个
href=""
与
onclick="window.location=''"
不仅如此,我还需要携带从 href 到 window.location 的链接。
例如,如果我有:
href="http://www.google.com.au"
它会变成:
onclick="window.location='http://www.google.com.au'"
我需要它在文档中的每个 href 上执行此操作
我不知道,但它需要在 jQuery/javascript 中:D
谢谢你们。
最佳答案
你可以试试这个:
$('a').each(function() {
var href = $(this).attr('href');
$(this).attr('onclick', "window.location='" + href + "'")
.removeAttr('href');
});
你想用这个实现什么?很可能有一种更优雅的方式来实现您所追求的目标。
比如在JS中自己处理事件可能会更好。将第三行替换为:
$(this).click(function() { window.location = href; })
但这可能会变得非常昂贵,因此您可能需要考虑 jQuery 的 delegate
事件:http://api.jquery.com/delegate/
关于javascript - jQuery 将所有 href =""替换为 onclick ="window.location=",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9406954/