html - 打开一个新页面 onclick onclick 一个 div 元素

我不知道这是否可行,但我正在尝试通过单击 HTML 元素来打开页面。我在下面详细说明。

我有一个 HTML 网页,其中包含来自第三方工具的源代码(用于联属网络营销的产品代码)。因此,当用户点击此代码中的其中一种产品时,他将被重定向到第三方商家。让我们想象一下代码如下:

<head>
<script async defer src="[linkremoved]"></script>
</head>
<body>
<!-- my page content -->
<div>
<!-- Partner code -->
<div data-gyg-href="[linkremoved]" data-gyg-locale-code="fr-FR" data-gyg-q="Neuhausen"></div>
</div>
<!-- my page content -->
</body>

我想要的是,如果用户点击产品,我的网站也会打开一个页面,其中包含类似“您能找到您的产品吗?我们会对您的反馈感兴趣”的文本。

我就是这样尝试的,但没有成功:

<head>
<script async defer src="[linkremoved]"></script>
</head>
<body>
<!-- my page content -->
<a href ="comments.html" target="_blank">
<div>
<!-- Partner code -->
<div data-gyg-href="[linkremoved]" data-gyg-locale-code="fr-FR" data-gyg-q="Neuhausen"></div>
</div></a>
<!-- my page content -->
</body>

最佳答案

如果我认为最简单的方法就是使用 javascript,请参见下面的示例。

// If you want to redirect user without opening a new tab, use this
// window.location.href="https://example.com";

// But if you want to open a new tab, use this
// window.open("https://www.example.com", "_blank");

function noNewTab() {
  window.location.href="https://example.com";
}

function newTab() {
  window.open("https://www.example.com", "_blank");
}

function localFile() {
   // First we will need to get the url of your webpage
   let currentUrl = window.location.origin;
   // To what file redirect?
   let redirectFile = "test.html";

   // And redirect. We will need to add / before the file path
   window.location.href = currentUrl + "/" + redirectFile;
   
}
<div onclick="noNewTab()">
  <p> Example product. Click on me! (No new tab) </p>
</div>

<div onclick="newTab()">
  <p> Example product. Click on me! (New tab) </p>
</div>

<div onclick="localFile()">
  <p> Example product. Click on me! (Change file, not url) </p>
</div>

对于新标签,它取决于浏览器是否允许显示它。在现代浏览器上,弹出窗口要求访问

https://stackoverflow.com/questions/73455880/

相关文章:

xcode - 错误构建 : Command PhaseScriptExecution failed

javascript - 类型错误 : Cannot read properties of unde

javascript - 如何从脚本标签和/或控制台中抓取值

android - 在 Stripe 付款上显示每小时价格而不是存款价格

android - Proguard R8 警告

.net - 如何写入 Windows 事件查看器应用程序 channel ? (。网)

postgresql - 错误 : connect ECONNREFUSED 127. 0.1.1:

reactjs - react ,状态变化没有反射(reflect)在回调函数中

swift - 填充颜​​色影响 SwiftUI 中的阴影

reactjs - Github 中This branch has not been deploye