jQuery替换所有href =“"使用onclick =" window.location =" [英] jQuery replace all href="" with onclick="window.location="
问题描述
所以我有一个不错的选择.
So I have a cool one for ya.
我需要扫描正在渲染的html文档,并替换所有
I need to scan my html document as it is rendering and replace every
href=""
使用
onclick="window.location=''"
除此之外,我还需要携带从href到window.location的链接.
more than that, I need to carry the link from the href to the window.location.
例如,如果我有:
href="http://www.google.com.au"
它将变成:
onclick="window.location='http://www.google.com.au'"
,我需要它对文档中的每个href
我不知道,但是它必须在jQuery/javascript:D
I have no idea, but it needs to be in jQuery / javascript :D
谢谢大家.
推荐答案
您可以尝试以下方法:
$('a').each(function() {
var href = $(this).attr('href');
$(this).attr('onclick', "window.location='" + href + "'")
.removeAttr('href');
});
您打算如何实现?可能有一种更优雅的方式来实现自己的目标.
What are you trying to achieve with this? Chances are that there's a more elegant way to achieve what you're after.
例如,最好自己在JS中处理事件.将第三行替换为:
For example, it might be better to handle the event yourself in the JS. Replace the third line with:
$(this).click(function() { window.location = href; })
这可能会变得非常昂贵,因此您可能需要考虑jQuery的delegate
事件: http://api.jquery.com/delegate/
That could become very expensive though, so you might want to consider jQuery's delegate
events: http://api.jquery.com/delegate/
这篇关于jQuery替换所有href =“"使用onclick =" window.location ="的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!