简体   繁体   English

当已选择数组中的DOM元素时,如何阻止javascript函数运行?

[英]How do I prevent a javascript function from running when a DOM element from an array is already selected?

I have a script that makes gives me a dynamic header depending on which bubble in an array is currently selected. 我有一个脚本,该脚本根据当前选择数组中的哪个气泡为我提供了动态标头。 However, when a bubble is already selected and the user clicks on that same bubble, I can't stop the js function from running and it changes the opacity of the header to 0 temporarily. 但是,当已经选择一个气泡并且用户单击相同的气泡时,我无法停止js函数的运行,并且它会暂时将标头的不透明度更改为0。 In this case I'd like for the opacity of the header to remain as 1. This seems rather simple but I can't figure it out. 在这种情况下,我希望标题的不透明度保持为1。这似乎很简单,但我无法弄清楚。 The code on CodePen. CodePen上的代码。

HTML: HTML:

<div id="bubbles">
<div onclick="bubbles(0); clearInterval(intrvl);" style="background:#da2225;"></div>
<div onclick="bubbles(1); clearInterval(intrvl);"></div>
<div onclick="bubbles(2); clearInterval(intrvl);"></div>
</div>
<div id="bubblecontent">
<h2>Header 1</h2>
</div>

Javascript: Javascript:

function _(x){return document.getElementById(x);}

var ba, bi=0, intrvl;
var bca = [
'<h2>Header 1</h2>',
  '<h2>Header 2</h2>',
  '<h2>Header 3</h2>',
];

function bubbles(bi){
_("bubblecontent").style.opacity = 0;
for(var i=0; i < ba.length; i++){
    ba[i].style.background = "#ccc";
}
ba[bi].style.background = "#da2225";
setTimeout(function(){
    _("bubblecontent").innerHTML = bca[bi];
    _("bubblecontent").style.opacity = 1;
}, 300);
}

window.addEventListener("load", function(){
ba = _("bubbles").children;
});

In function bubbles, add conditional if the bi is the same as previous one: 在功能气泡中,如果bi与上一个相同,则添加条件:

var prev = -1;

function bubbles(bi){
   if (bi != prev){
//           put the rest of your bubbles(bi) code put here
   }
   prev = bi;
}

You can use HTMLElement.dataset to store variable of active element, set first <div> child of #bubbles element to "true" , at click event of div element set all #bubbles div children ba dataset.active to "false" , set current element ba[bi] .dataset.active to "true" 您可以使用HTMLElement.dataset存储有源元件的变量,第一组<div>的孩子#bubbles元素"true" ,在click的事件div设置的所有元素#bubbles divba dataset.active"false" ,设置当前元素ba[bi] .dataset.active"true"

 function _(x) { return document.getElementById(x); } var ba, bi = 0, intrvl; var bca = [ '<h2>Header 1</h2>', '<h2>Header 2</h2>', '<h2>Header 3</h2>', ]; function bubbles(bi) { if (ba[bi].dataset.active !== "true") { _("bubblecontent").style.opacity = 0; for (var i = 0; i < ba.length; i++) { ba[i].style.background = "#ccc"; ba[i].dataset.active = "false"; } ba[bi].dataset.active = true; ba[bi].style.background = "#da2225"; setTimeout(function() { _("bubblecontent").innerHTML = bca[bi]; _("bubblecontent").style.opacity = 1; }, 300); } } window.addEventListener("load", function() { ba = _("bubbles").children; }); 
 #bubbles { text-align: center; } #bubbles > div { display: inline-block; width: 15px; height: 15px; margin: 24px 10px 0px 10px; background-color: #ccc; border-radius: 100%; transition: background 0.3s linear 0s; cursor: pointer; } #bubblecontent { transition: opacity 0.3s linear 0s; } #bubblecontent h2 { text-align: center; color: #333; } 
 <div id="bubbles"> <div data-active="true" onclick="bubbles(0); clearInterval(intrvl);" style="background:#da2225;"></div> <div onclick="bubbles(1); clearInterval(intrvl);"></div> <div onclick="bubbles(2); clearInterval(intrvl);"></div> </div> <div id="bubblecontent"> <h2>Header 1</h2> </div> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 单击选定元素内部的生成字段时,如何防止jQuery .click函数运行? - How to prevent jQuery .click function from running when clicking in generated field inside selected element? 当响应插入DOM时,如何防止从ajax请求返回的javascript执行? - How do I prevent javascript returned from an ajax request from executing when the response is inserted into the DOM? 如何防止html / javascript代码在文本框中运行 - How do i prevent html/javascript code from running on a textbox 如何防止 javascript 解构赋值中的 function 参数被具有相同 ID 的 DOM 元素覆盖 - how to prevent a function parameter in javascript destructuring assignment from being overrided by DOM element with same ID 如何从JavaScript中的数组访问DOM节点? - How do I access DOM nodes from an array in javascript? 如何防止 ReactJS onclick function 在渲染上运行 - How do I prevent ReactJS onclick function from running on render Javascript定时器已经在运行,只有在刷新时才会重复时间,如何防止时间重复 - Javascript timer is already running only when it is refreshed the time is repeated, how to prevent time from being repeated javascript:如何更改我的获取选择函数,以便不从选择中的第一个DOM元素中删除额外的空格? - javascript: How do I change my get selection function to not remove extra whitespace from the first DOM element in the selection? 如何检测用户何时单击了从选择中选择的元素? - How to detect when user clicks on an element already selected from a select? 如何在javascript中将元素从数组移动到另一个数组? - How do I move an element from an array to another array in javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM