我想让用户去一些网站当他点击我的网站背景图片(父div),但功能需要关闭时,用户点击我的网站内容(子div),然后当用户再次点击背景图片(父div),该功能将是活动。
我尝试了很多次,但当我点击我的网站内容(子div),并再次点击网站背景图片(Parent div)时,该功能不起作用。
我能做些什么?谢谢!
下面是我的代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"></meta>
<title>Test!</title>
<style type="text/css">
.head{
background-color:#FF88C2; color:#9955FF; font-weight:bold; font-size:30px;
text-align:center; padding:10px;
}
.tt{
width:100%; height:100%;
border:1px solid red;
}
.content{
width:800px; height:1000px; margin-left:auto; margin-right:auto; background-color: aliceblue;
border:1px solid blue;
}
</style>
<script type="text/javascript">
function bgADlink(){
window.open('https://www.google.com');
}
var i = 0;
function test1(){
if(i == 0 || i == 1)
bgADlink();
return i = 0;
}
function test2(){
bgADlink=false;
return i = 1;
}
</script>
</head>
<body style="margin:0px; background-color:#eeeeee;">
<div class="head">Test</div>
<div class="tt" onclick="test1()">
<div class="content" onclick="test2()"></div>
</div>
</body>
</html>发布于 2019-01-04 19:18:45
只需从test1()函数中删除|| i == 1条件,如下所示。
function test1(){
if(i == 0)
bgADlink();
return i = 0;
}检查此fiddle https://jsfiddle.net/ca7Lbsy3/
发布于 2019-01-04 19:14:45
您的代码中存在不同的问题。
因为事件从一个内部元素传播到它的父元素(不仅是父元素,而且是所有向上的层次结构),所以"tt“div总是会触发,所以最好的做法是用以下命令停止事件的传播
onclick="event.stopPropagation();test1()"我真的不明白你想用"i“来完成什么,为什么你有一个函数bgADlink(),然后给它赋值一个布尔值,JavaScript可以让你做到这一点,但基本上你的函数现在是一个布尔值。
我给你做了一个小提琴手的小样本,你可以看到stopPropagation在工作,希望它能帮上忙。
发布于 2019-01-04 19:16:52
此代码段未打开父div链接either..but在html页面上使用此链接,然后尝试使用它
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"></meta>
<title>Test!</title>
<style type="text/css">
.head{
background-color:#FF88C2; color:#9955FF; font-weight:bold; font-size:30px;
text-align:center; padding:10px;
}
.tt{
width:100%; height:100%;
border:1px solid red;
}
.content{
width:800px; height:1000px; margin-left:auto; margin-right:auto; background-color: aliceblue;
border:1px solid blue;
}
</style>
<script type="text/javascript">
var i = 0;
function test1(){
if(i == 0 || i == 1)
bgADlink();
return i = 0;
}
function test2(varr,event){
// bgADlink=false;
event.stopPropagation();
return i = 1;
}
function bgADlink(){
console.log("bgADlink")
window.open('https://www.google.com');
}
</script>
</head>
<body style="margin:0px; background-color:#eeeeee;">
<div class="head">Test</div>
<div class="tt" onclick="test1()">
<div class="content" onclick="test2('',event)"></div>
</div>
</body>
</html>
https://stackoverflow.com/questions/54037270
复制相似问题