gpt4 book ai didi

javascript - 仅在隐藏 div 时运行脚本(显示 :none) is active

转载 作者:行者123 更新时间:2023-11-28 01:06:29 24 4
gpt4 key购买 nike

我只需要在隐藏的 div (display:none) 处于事件状态时运行脚本。

当按下“radio button”时,假加载程序显示“div1”,3 秒后显示另一个名为“my_div”的 div。

但是如果用户不按下“单选按钮”,“my_div”上的随机文本仍然会出现。

我需要解决这个问题,如果用户按下单选按钮,只显示这个随机文本“my_div”。因此,当用户按下“单选按钮”时,使用假加载器显示“div1”,3 秒后,显示另一个 div(已经工作),如果不按下按钮,则什么也不会发生(不要运行随机脚本)。

工作: https://jsfiddle.net/zto6gv1c/3/

这是我的元素:

function show1() {
document.getElementById('div1').style.display = 'none';
}

function show2() {
document.getElementById('div1').style.display = 'block';
}

var r_text = new Array();
r_text[0] = "Disponível";
r_text[1] = "Indisponível";
r_text[2] = "Disponível";
r_text[3] = "Indisponível";
r_text[4] = "Disponível";
r_text[5] = "Disponível";
r_text[6] = "Indisponível";
r_text[7] = "Disponível";
r_text[8] = "Indisponível";
r_text[9] = "Disponível";
r_text[10] = "Indisponível";
r_text[11] = "Disponível";
var i = Math.floor(7 * Math.random())

document.write(r_text[i]);

window.onload = function() //executes when the page finishes loading
{
setTimeout(func1, 3000); //sets a timer which calls function func1 after 2,000 milliseconds = 2 secs.

};

function func1() {
document.getElementById("my_div").className = "show";
}
.hide {
display: none;
}

p {
font-weight: bold;
}

.show {
display: block;
position: absolute;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<p>Deseja chegar a disponibilidade do produto?</p>

<input type="radio" name="tab" value="igottwo" onclick="show2();" /> Sim

<div id="div1" class="hide">CHECANDO</div>

<div id="my_div" class="hide"></div>

最佳答案

希望这就是您要找的。如果我在附近,请告诉我。

function show2() {
document.getElementById('div1').style.display = 'block';
setTimeout(func1, 3000);
}

然后,将 document.write 替换为以下内容

document.getElementById("my_div").innerHTML = r_text[i];

然后将您的 window.onload 代码替换为下面的代码

window.onload = function() //executes when the page finishes loading
{
setTimeout(function(){
if(document.getElementById('div1').style.display != 'block'){
func1();
}
}, 3000); //sets a timer which calls function func1 after 2,000 milliseconds = 2 secs.
};

关于javascript - 仅在隐藏 div 时运行脚本(显示 :none) is active,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52333023/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com