如何在 onclick 事件前面加上一串文本
我有带有 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);
谢谢。
I have <a>
s with onclick events:
<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>
How can I prepend those onclick events with http://www.example.com
so that the result will be:
<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>
It has to be compatible with jQuery 1.4.2
Somebody else, had mentioned something like this, but I can't get it to work in 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);
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为
window.open()
在这里不是一个好主意。它会被大多数弹出窗口拦截器拦截,如果您使用 jQuery,则无论如何都不应该使用内联onclick
事件。您想要做的事情可能可以通过一个简单的锚链接来实现:
然后您可以执行以下操作:
编辑:
如果您无法控制 html 并且需要执行以下操作就像这样,然后在
onClick
属性上使用replace()
,例如:example: http://jsfiddle.net/elclanrs/AH4As/
I don't think
window.open()
is a good idea here. It will get blocked by most popup blockers and if you're using jQuery you shouldn't be using the inlineonclick
event anyway.What you're trying to do can probably be achieved with a simple anchor link:
And then you can do something like this:
EDIT:
If you don't have control over the html and you need to do it like that then use
replace()
on theonClick
attribute like:example: http://jsfiddle.net/elclanrs/AH4As/