【问题标题】:Open link in background tab without losing focus在后台选项卡中打开链接而不会失去焦点
【发布时间】:2020-02-27 11:56:18
【问题描述】:

我在我的网站上制作了一个显示优惠券代码的模式弹出窗口,该按钮链接到优惠页面。我希望优惠页面在后台选项卡中打开,而不会失去对我页面的关注。有什么办法吗?这是我在按钮显示优惠券代码并购买上的模式弹出窗口的工作link,谢谢。

function copyToClipboard(element) {
  var $temp = $("<input>");
  $("body").append($temp);
  $temp.val($(element).text()).select();
  document.execCommand("copy");
  $temp.remove();
  alert("Text Copied");
}
.coup-button {
  font-size: 1.25em;
  padding: 15px;
  background: #dc3545;
  color:#fff;
  border-radius: 8px;
  text-decoration: none;
  cursor: pointer;
  transition: all 0.3s ease-out;
}
.coup-button:hover {
  border: #dc3545 solid;
  color:#dc3545;
  background: #fff;
}

.coup-overlay {
  position: fixed;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  background: rgba(0, 0, 0, 0.7);
  transition: opacity 500ms;
  visibility: hidden;
  opacity: 0;
}
.coup-overlay:target {
  visibility: visible;
  opacity: 1;
}

.coup-popup {
  margin: 70px auto;
  padding: 20px;
  background: #fff;
  border-radius: 5px;
  width: 30%;
  position: relative;
  transition: all 5s ease-in-out;
}

.coup-popup h4 {
  margin-top: 0;
  color: #333;
}
.coup-popup .coup-close {
  position: absolute;
  top: 20px;
  right: 30px;
  transition: all 200ms;
  font-size: 30px;
  font-weight: bold;
  text-decoration: none;
  color: #333;
}
.coup-popup .coup-close:hover {
  color: #dc3545;
}
.coup-popup .coup-content {
  max-height: 30%;
  overflow: auto;
}

.coup-copybutton{width: auto; height: auto; background: #dc3545; color: white; border: none;cursor: pointer; border-radius: 8px; outline: none; padding:10px; box-sizing: border-box;}

@media screen and (max-width: 700px){
  .coup-popup{
    width: 70%;
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<a class="coup-button" href="https://www.pmbypm.com/best-pmp-exam-simulator/" target="_blank" onclick="location.href='#popup1'">Reveal Coupon Code</a>
<div id="popup1" class="coup-overlay">
	<div class="coup-popup">
		<h4>Here is your Coupon Code</h4>
		<a class="coup-close" href="#">&times;</a>
		<div class="coup-content" style="text-align: center;">
			<p id="p1"><strong>UDEMYPM30</strong></p>
			<button class="coup-copybutton" onclick="copyToClipboard('#p1')">Copy Text</button>
		</div>
	</div>
</div>

【问题讨论】:

  • 所以你正试图在后台打开新标签,对吧?
  • 是的,就像您在按住 ctrl 键时打开链接
  • 我不认为这是可能的,不是在所有浏览器中,也不是今天。在后台打开新标签会导致用户没有注意到它。想想这会导致多少(性能或安全)问题。一些讨厌的网站过去常常在后台打开几个广告标签,您必须稍后再处理这些标签。不这样做的原因有很多。

标签: javascript jquery html tabs focus


【解决方案1】:

试试window.open('link', 'name');

function copyToClipboard(element) {
  var $temp = $("<input>");
  $("body").append($temp);
  $temp.val($(element).text()).select();
  document.execCommand("copy");
  $temp.remove();
  alert("Text Copied");
}

function openView() {
  location.href='#popup1';
  window.open('https://stackoverflow.com/questions/60432716/', 'name'); 
}
.coup-button {
  font-size: 1.25em;
  padding: 15px;
  background: #dc3545;
  color:#fff;
  border-radius: 8px;
  text-decoration: none;
  cursor: pointer;
  transition: all 0.3s ease-out;
}
.coup-button:hover {
  border: #dc3545 solid;
  color:#dc3545;
  background: #fff;
}

.coup-overlay {
  position: fixed;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  background: rgba(0, 0, 0, 0.7);
  transition: opacity 500ms;
  visibility: hidden;
  opacity: 0;
}
.coup-overlay:target {
  visibility: visible;
  opacity: 1;
}

.coup-popup {
  margin: 70px auto;
  padding: 20px;
  background: #fff;
  border-radius: 5px;
  width: 30%;
  position: relative;
  transition: all 5s ease-in-out;
}

.coup-popup h4 {
  margin-top: 0;
  color: #333;
}
.coup-popup .coup-close {
  position: absolute;
  top: 20px;
  right: 30px;
  transition: all 200ms;
  font-size: 30px;
  font-weight: bold;
  text-decoration: none;
  color: #333;
}
.coup-popup .coup-close:hover {
  color: #dc3545;
}
.coup-popup .coup-content {
  max-height: 30%;
  overflow: auto;
}

.coup-copybutton{width: auto; height: auto; background: #dc3545; color: white; border: none;cursor: pointer; border-radius: 8px; outline: none; padding:10px; box-sizing: border-box;}

@media screen and (max-width: 700px){
  .coup-popup{
    width: 70%;
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<a class="coup-button" href="https://www.pmbypm.com/best-pmp-exam-simulator/" target="_blank" onclick="openView()">Reveal Coupon Code</a>
<div id="popup1" class="coup-overlay">
	<div class="coup-popup">
		<h4>Here is your Coupon Code</h4>
		<a class="coup-close" href="#">&times;</a>
		<div class="coup-content" style="text-align: center;">
			<p id="p1"><strong>UDEMYPM30</strong></p>
			<button class="coup-copybutton" onclick="copyToClipboard('#p1')">Copy Text</button>
		</div>
	</div>
</div>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-02
    • 1970-01-01
    • 1970-01-01
    • 2011-05-25
    • 1970-01-01
    相关资源
    最近更新 更多