Javascript 如果选中复选框,则在新窗口中打开链接

Javascript 如果选中复选框,则在新窗口中打开链接,javascript,html,Javascript,Html,因此,我想让我的用户可以根据复选框值在新窗口或相同窗口中打开链接。如果选中该值,则在新窗口中打开,如果未在同一窗口中打开。如果可能的话,我希望在进行选择时不必重新加载页面。您应该使用link属性 函数setLinkTarget(复选框,选择器){ var link=document.querySelector(选择器); 如果(链接){ link.target=checkbox.checked?“\u blank”:“u self”; } } 您应该使用link属性 函数setLinkTa

因此,我想让我的用户可以根据复选框值在新窗口或相同窗口中打开链接。如果选中该值,则在新窗口中打开,如果未在同一窗口中打开。如果可能的话,我希望在进行选择时不必重新加载页面。

您应该使用link属性

函数setLinkTarget(复选框,选择器){
var link=document.querySelector(选择器);
如果(链接){
link.target=checkbox.checked?“\u blank”:“u self”;
}
}

您应该使用link属性

函数setLinkTarget(复选框,选择器){
var link=document.querySelector(选择器);
如果(链接){
link.target=checkbox.checked?“\u blank”:“u self”;
}
}

JavaScript/Jquery—这是一种使用Jquery查看复选框是否选中的方法,取决于复选框是否有两个不同的锚定标记,它们将在新选项卡或同一选项卡中打开

//Animated Checkbox
//Start by showing the open in same tab link
var b;
b = '<a href="https://www.google.com/" target="_self">Open In Same    Tab</a>'
document.getElementById('samelink').innerHTML = b;

$("#samelink").show();
$("#link").hide(); 


//Check if the checkbox was clicked or checked
$('#check').click(function() {
//If it is create the anchor tag and substitute it into the html
if(document.getElementById('check').checked) {

var c;
c = '<a href="https://www.google.com/" target="_blank">Open In New Tab</a>'
document.getElementById('link').innerHTML = c;

$("#samelink").hide();
$("#link").show();
} else {
//If it hasn't been checked keep the open in same tab link in there
var b;
b = '<a href="https://www.google.com/" target="_self">Open In Same Tab</a>'
document.getElementById('samelink').innerHTML = b;

$("#samelink").show();
$("#link").hide();
}
}); 
//动画复选框
//首先显示“在同一选项卡中打开”链接
var b;
b=“”
document.getElementById('samelink')。innerHTML=b;
$(“#samelink”).show();
$(“#链接”).hide();
//检查复选框是否已单击或选中
$(“#检查”)。单击(函数(){
//如果是,则创建锚定标记并将其替换为html
if(document.getElementById('check').checked){
var c;
c=''
document.getElementById('link').innerHTML=c;
$(“#samelink”).hide();
$(“#链接”).show();
}否则{
//如果未选中,请在同一选项卡中保持打开链接
var b;
b=“”
document.getElementById('samelink')。innerHTML=b;
$(“#samelink”).show();
$(“#链接”).hide();
}
}); 
HTML部分

<!-- Create the form -->
<div class="checkbox">
<form>
<label>
<input type="checkbox" name="radio" id="check" class="checkb"><span class="label-text">Open in new window?</span></input>
</label>

</form>

<div class="linked">
<p id="link"></p><!-- if open in new tab is true put the link in this p tag, else put the link in the samelink p tag-->
<p id="samelink"></p>
</div>

</div>

在新窗口中打开?


JavaScript/Jquery—这是一种使用Jquery查看复选框是否选中的方法,取决于复选框是否有两个不同的锚定标记,它们将在新选项卡或同一选项卡中打开

//Animated Checkbox
//Start by showing the open in same tab link
var b;
b = '<a href="https://www.google.com/" target="_self">Open In Same    Tab</a>'
document.getElementById('samelink').innerHTML = b;

$("#samelink").show();
$("#link").hide(); 


//Check if the checkbox was clicked or checked
$('#check').click(function() {
//If it is create the anchor tag and substitute it into the html
if(document.getElementById('check').checked) {

var c;
c = '<a href="https://www.google.com/" target="_blank">Open In New Tab</a>'
document.getElementById('link').innerHTML = c;

$("#samelink").hide();
$("#link").show();
} else {
//If it hasn't been checked keep the open in same tab link in there
var b;
b = '<a href="https://www.google.com/" target="_self">Open In Same Tab</a>'
document.getElementById('samelink').innerHTML = b;

$("#samelink").show();
$("#link").hide();
}
}); 
//动画复选框
//首先显示“在同一选项卡中打开”链接
var b;
b=“”
document.getElementById('samelink')。innerHTML=b;
$(“#samelink”).show();
$(“#链接”).hide();
//检查复选框是否已单击或选中
$(“#检查”)。单击(函数(){
//如果是,则创建锚定标记并将其替换为html
if(document.getElementById('check').checked){
var c;
c=''
document.getElementById('link').innerHTML=c;
$(“#samelink”).hide();
$(“#链接”).show();
}否则{
//如果未选中,请在同一选项卡中保持打开链接
var b;
b=“”
document.getElementById('samelink')。innerHTML=b;
$(“#samelink”).show();
$(“#链接”).hide();
}
}); 
HTML部分

<!-- Create the form -->
<div class="checkbox">
<form>
<label>
<input type="checkbox" name="radio" id="check" class="checkb"><span class="label-text">Open in new window?</span></input>
</label>

</form>

<div class="linked">
<p id="link"></p><!-- if open in new tab is true put the link in this p tag, else put the link in the samelink p tag-->
<p id="samelink"></p>
</div>

</div>

在新窗口中打开?


听起来像是javascript的问题@丹尼拉,怀特,我忘了给它贴标签了吗?听起来像是javascript的问题@丹尼拉,怀特,我忘了给它贴标签了吗?哎呀。