如果地理位置 IP = NZ 则隐藏 div

问题描述 投票:0回答:1

如果人们从新西兰查看我们的网站 (www.1907water.com),我会尝试隐藏“ohi_widget”div。

这可能吗?如果可以,需要什么代码?

谢谢

我尝试了 Google 搜索、在线聊天、Stack Overflow 搜索。我尝试过一种“解决方案”,但它在我们的网站上不起作用,可能是因为我必须自定义它,而且我不懂 javascript。

javascript jquery geolocation
1个回答
0
投票

您可以使用ipapi.co:

fetch('https://ipapi.co/json/')
  .then(response => response.json())
  .then(data => {
    // Check if the country is New Zealand and hide div
    if (data.country === 'NZ') {
      document.getElementById('myDiv').style.display = 'none';
    }
  })
  .catch(error => {
    console.error('Error fetching IP data:', error);
  });
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Hide Div for NZ IPs</title>
</head>
<body>
  <div id="myDiv">
    This div will be hidden if your IP says you are in New Zealand.
  </div>
  <script src="script.js"></script>
</body>
</html>

© www.soinside.com 2019 - 2024. All rights reserved.