如何使用javascript更改地址的某些部分并导航到该地址?

如何使用javascript更改地址的某些部分并导航到该地址?,javascript,Javascript,例如,如果我想改变什么- www.site.com/img-small-1.jpg 到 如何使用javascript实现这一点 您可以使用replace更改地址 var currentAddress = "www.site.com/img-small-1.jpg"; var newAddress = currentAddress.replace("img-small-1.jpg", "img-large-1.jpg"); window.location.href = newAddress; /

例如,如果我想改变什么-

www.site.com/img-small-1.jpg


如何使用javascript实现这一点

您可以使用replace更改地址

var currentAddress = "www.site.com/img-small-1.jpg";
var newAddress = currentAddress.replace("img-small-1.jpg", "img-large-1.jpg");
window.location.href = newAddress; // this will redirect to new address

您可以在javascript中使用replace,有什么问题吗?我看不到您的代码中有任何错误。www.site.com/img-small-1.jpg.replaceSall,large
var currentAddress = "www.site.com/img-small-1.jpg";
var newAddress = currentAddress.replace("img-small-1.jpg", "img-large-1.jpg");
window.location.href = newAddress; // this will redirect to new address