如何使用JQuery将一个URL更改为另一个URL

如何使用JQuery将一个URL更改为另一个URL,jquery,safari-extension,Jquery,Safari Extension,我有以下脚本: <script> safari.application.addEventListener("command", performCommand, false); function performCommand(event) { if (event.command == "change") { $('a[href="http://example.com"]').attr('href', 'http://sub.example.co

我有以下脚本:

<script>  
safari.application.addEventListener("command", performCommand, false);  

function performCommand(event) {    
    if (event.command == "change") {  


    $('a[href="http://example.com"]').attr('href', 'http://sub.example.com');


    }  
}  
</script>

safari.application.addEventListener(“命令”,performCommand,false);
函数性能命令(事件){
如果(event.command==“change”){
$('a[href=”http://example.com“]”).attr('href','http://sub.example.com');
}  
}  

我想做的是,当按下菜单栏按钮时,它会运行中间的代码(在本例中,
('a[href='http://example.com“]”).attr('href','http://sub.example.com“);
)查找所有链接并用修改后的版本替换它们。如何完成此操作?

您可能需要更改选择器,使其仅查找以您想要的域开头的链接。这可以通过使用^=
而不仅仅是
=`来实现:

$('a[href^="http://example.com"]').attr('href',function(i,e){
  // we use a function so we can modify it instead of overwrtie
  return e.replace('example.com','google.com');
});

你能解释一下你的问题的一些症状吗?什么似乎是错误的/无效的/根本不工作的?嗯,当我使用它作为safari扩展时,它调用脚本,但不调用函数。嗯,我只想用替换。我在主要帖子中输入了错误。因此,将
.replace
修改为:
.replace('//example.com'//sub.example.com')
好的,我这样做了,但是当按下菜单栏按钮时脚本仍然没有运行该功能。我假设您在mac上,给定safari和“command”绑定,因此修复链接替换是我能做的最远的事情,对不起-\