所以我有一个很酷的给你。
我需要在渲染时扫描我的 html 文档并替换每个
href=""
with
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; })
但这可能会变得非常昂贵,因此您可能需要考虑 jQuerydelegate
事件:http://api.jquery.com/delegate/ http://api.jquery.com/delegate/
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)