如何在 onclick 事件前面加上一串文本

发布于 2025-01-05 01:22:26 字数 1118 浏览 0 评论 0原文

我有带有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

放赐 2025-01-12 01:22:26

我认为 window.open() 在这里不是一个好主意。它会被大多数弹出窗口拦截器拦截,如果您使用 jQuery,则无论如何都不应该使用内联 onclick 事件。
您想要做的事情可能可以通过一个简单的锚链接来实现:

<a href="trackPackage.asp" target="_blank"></a>

然后您可以执行以下操作:

var prependUrl = function($link, url) {
    var oldUrl = $link.attr('href'),
        newUrl = url + oldUrl;
    $link.attr('href', newUrl);
}

prependUrl($('#yourLink'), 'http://www.example.com/');

编辑:

如果您无法控制 html 并且需要执行以下操作就像这样,然后在 onClick 属性上使用 replace() ,例如:

$('a').attr('onClick', $('a').attr('onClick').replace('window.open(\'', 'window.open(\'http://example.com/'));

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 inline onclick event anyway.
What you're trying to do can probably be achieved with a simple anchor link:

<a href="trackPackage.asp" target="_blank"></a>

And then you can do something like this:

var prependUrl = function($link, url) {
    var oldUrl = $link.attr('href'),
        newUrl = url + oldUrl;
    $link.attr('href', newUrl);
}

prependUrl($('#yourLink'), 'http://www.example.com/');

EDIT:

If you don't have control over the html and you need to do it like that then use replace() on the onClick attribute like:

$('a').attr('onClick', $('a').attr('onClick').replace('window.open(\'', 'window.open(\'http://example.com/'));

example: http://jsfiddle.net/elclanrs/AH4As/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文