请支持在HTML代码中为我的JS文件添加网络交换机。仅举一个例子就足够了。
var bep20Button = document.querySelector('.network-bep20-token');
var matic20Button = document.querySelector('.network-matic-token');
function changeToBepNetwork() {
bep20Button.classList.add("network-active-tab");
matic20Button.classList.remove("network-active-tab");
tokenCurrency.innerHTML = "BNB";
}发布于 2022-06-01 10:57:16
开始吧。下面的函数使用元and,并将网络切换到函数参数中接收的网络。请注意,网络id必须以0x格式传递,例如,example的0x1。
// switches network to the one provided. Must be in 0x format
const switchNetwork = async (chainId) => {
await window.ethereum.request({
method: 'wallet_switchEthereumChain',
params: [{ chainId: chainId}],
})
// refresh
window.location.reload()
}发布于 2022-06-01 08:24:21
我认为您要寻找的是web3.js中的wallet_switchEthereumChain方法。您可以找到如何使用它的示例这里。
https://ethereum.stackexchange.com/questions/129291
复制相似问题