2011-02-11 56 views
1

我有以下腳本:如何一個網址更改爲另一個與jQuery

<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> 

而我希望發生的是,當按下菜單欄按鈕時,它運行在的代碼(在此case,('a[href="http://example.com"]').attr('href', 'http://sub.example.com');),它找到所有鏈接並用修改後的版本替換它們。我怎樣才能做到這一點?

+0

你能解釋你的問題的一些症狀嗎?什麼似乎是錯誤/無效/根本不工作? – 2011-02-11 21:16:54

+0

那麼,當我用它作爲safari擴展時,它會調用腳本,但它不會調用該函數。 – Charlie 2011-02-11 21:26:07

回答

0

您可能需要更改選擇器,以便僅查找以您想要的域名開頭的鏈接。這可以使用^ =​​=`:

$('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'); 
});