在 window.open 函数中添加 URL 前缀 jQuery
我有这个 HTML:
<a href="#" onclick="window.open('TrackPackage.asp?ID=4', '', 'settings');">Track Your Package »</a>
此站点上的某人能够为我提供一个脚本,以在 URL 中添加域前缀 http://www.example.com/
这是脚本:
$(document).ready(function(){
$('a[onclick^="window.open(\'TrackPackage.asp"]').attr('onClick', $('a[onclick^="window.open(\'TrackPackage.asp"]').attr('onClick').replace("window.open('", "window.open('http://www.example.com/"));
});
但是,我有有点麻烦:
第一个问题是元素有多个实例。这是一个小提琴: http://jsfiddle.net/VMmZx/
按照预期,一个锚点使用 ID=4
签名,另一个锚点使用 ID=5
签名,而是都使用 ID=4
签名>。
这个想法是,每个 window.open
函数都应该以 http://www.example.com
为前缀,但是 URL 的其余部分应该保持不变。 ..
我遇到的第二个问题是,当页面上不存在该元素时,jQuery 的其余部分将失败...
这是另一个小提琴: http://jsfiddle.net/VPf32/ 应该获取类
foo
,但由于页面上不存在该元素,因此 jQuery 不会执行。
由于 JavaScript 包含在 ASP.NET 服务器的 HTML 模板中,这可能会产生很多问题。
我希望我已经说清楚了,您可以帮助我。 谢谢。
I have this HTML:
<a href="#" onclick="window.open('TrackPackage.asp?ID=4', '', 'settings');">Track Your Package »</a>
Somebody on this site was able to provide me with a script to prefix the URL with the domain http://www.example.com/
Here's the script:
$(document).ready(function(){
$('a[onclick^="window.open(\'TrackPackage.asp"]').attr('onClick', $('a[onclick^="window.open(\'TrackPackage.asp"]').attr('onClick').replace("window.open('", "window.open('http://www.example.com/"));
});
However, I am having a little trouble with this:
The first issue is where there is multiple instances of the element. Here's a fiddle: http://jsfiddle.net/VMmZx/
Instead of one anchor being signed with ID=4
and the other with ID=5
as intended, they're both being signed with ID=4
.
The idea is, each window.open
function should be prefixed with http://www.example.com
however, the remainder of the URL should remain intact...
The second problem I'm encountering is when the element does not exist on a page, the remainder of the jQuery fails...
Here's another fiddle: http://jsfiddle.net/VPf32/
The <a>
should get the class foo
, but since the element does not exist on the page, the jQuery does not execute.
Since the JavaScript is being included in the HTML template of the ASP.NET server, this can create many problems.
I hope I've been clear and you can help me. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
.each()
迭代每个匹配元素并更改它们单独:但是,我认为使用
href
为#
和onclick
打开窗口的链接并不具有语义性。如果可能的话,尝试将标记更改为:现在,如果有人好奇它将引导他们去哪里,当您将鼠标悬停在状态栏中时,浏览器可以在状态栏中显示一些有用的内容。
如果您需要进一步调整行为,请添加一个类并绑定
click
事件。当它们单击时,阻止默认操作并自行打开窗口,就像您之前所做的那样。You can use
.each()
to iterate over each matching element and change them individually:However, I don't think using links with a
href
of#
and anonclick
opening a window is as semantic as it could be. If possible, try changing the markup to this:Now if someone is curious where it will lead them, the browser can show something useful in the status bar when you hover over it.
If you need to adjust the behavior further, add a class and bind for the
click
event. When they click, prevent the default action and open the window yourself, as you did before.你为什么要这样进行内联点击?我只会输出如下链接:
然后:
Why are you doing the click even inline like that? I would just output the links like:
And then: