我有<a>
onclick 事件:
<a href="#" onclick="window.open('TrackPackage.asp', '', 'location=1,menubar=1,scrollbars=1,status=1,resizable=1,width=635,height=460'); return false;" class="nounderline">Track Your Package »</a>
我怎样才能在这些 onclick 事件之前加上http://www.example.com
这样的结果:
<a href="#" onclick="window.open('http://www.example.com/TrackPackage.asp', '', 'location=1,menubar=1,scrollbars=1,status=1,resizable=1,width=635,height=460'); return false;" class="nounderline">Track Your Package »</a>
它必须与 jQuery 1.4.2 兼容
其他人提到过这样的事情,但我无法让它在 1.4.2 中工作:
var link = $("a"); // I don't have enough info to tell you how to precisely get this instance
var originalOnClick = link.attr("onclick");
var part1 = "window.open('"; // this is always the same, right?
var part2 = originalOnClick.substr(part1.length); // the remainder, beginning with TrackPackage.asp
var newOnClick = part1 + "http://www.example.com/" + part2;
link.attr("onclick", newOnClick);
谢谢。