Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在JavaScript中保留一个活动按钮?_Javascript_Angularjs_Html_Expression - Fatal编程技术网

如何在JavaScript中保留一个活动按钮?

如何在JavaScript中保留一个活动按钮?,javascript,angularjs,html,expression,Javascript,Angularjs,Html,Expression,我是初学者。 我有四个按钮,我想保留一个积极的按钮,每次与表达式运算符如果。每次必须有一个按钮处于活动状态。 我试着这样做。我愿意接受你的想法,如果你不需要的话。帮帮我 var计数=4; var标志=真; 功能选择CurrentColor,changeColor{ ifcount>1&&flag===true{ var currentElement=angular.elementdocument.getElementsByClassNamecurrentColor; currentElemen

我是初学者。 我有四个按钮,我想保留一个积极的按钮,每次与表达式运算符如果。每次必须有一个按钮处于活动状态。 我试着这样做。我愿意接受你的想法,如果你不需要的话。帮帮我

var计数=4; var标志=真; 功能选择CurrentColor,changeColor{ ifcount>1&&flag===true{ var currentElement=angular.elementdocument.getElementsByClassNamecurrentColor; currentElement.toggleClasschangeColor; 计数-; console.logcount; console.log'From减号:'+计数; }否则{ flag=false; } ifcount<4&&flag==false{ var currentElement=angular.elementdocument.getElementsByClassNamecurrentColor; currentElement.toggleClasschangeColor; 计数++; console.logcount; console.log'From plus:'+计数; }否则{ flag=true; } } .换颜色{ 颜色:红色!重要; } .首先{ 颜色:07888A; } .第二{ 颜色:07888A; } .第三{ 颜色:07888A; } .第四{ 颜色:07888A; } h1{ 显示:内联; 右边距:20px; } 第一 第二 第三 第四 添加此位:

function select(currentColor, changeColor) {
  // Get the list of the `.changeColor` elements.
  changed = document.querySelectorAll(".changeColor");
  // Loop through all the elements with `changeColor` class.
  for (i = 0; i < changed.length; i++)
    // Remove the changeColor class from their class list.
    changed[i].classList.remove("changeColor");

  // Rest comes your code.
  if(count > 1 && flag === true){

在启用任意三个按钮时,是否尝试禁用一个按钮?如果是这样的话,也许这会有所帮助。我强烈建议不要为此使用h1标记,使用按钮或div之类的东西,从元素中删除onclick属性,并将它们合并到主js文件中,类似于下面的js片段

(function() {
    //an empty array to track the number of elements currently colored
    var numberOfElesColored = [],
        eles = document.querySelectorAll('h1'),
        //the number of active elements allowed at once
        numberOfActiveElementsAllowed = eles.length - 1;


    //loop though all the elements and attach click event
    [].forEach.call(eles, function(ele, i) {

        ele.addEventListener('click', function(event) {

            var currentEle = event.target;


            //is there at least two other elements avaliable still ?
            if (!(numberOfElesColored.length === numberOfActiveElementsAllowed)) {
                //yes
                //is the current clicked element not active already ?
                if (!currentEle.classList.contains('changeColor')) {
                    //yes 

                    //add 1 to tracking array 
                    numberOfElesColored.push(1);
                    //activate element
                    return currentEle.classList.add('changeColor');
                } else {
                    //no

                    //remove 1 from tracking array
                    numberOfElesColored.pop();
                    //deactivate elements
                    return currentEle.classList.remove('changeColor');
                }

                //are all the elements active already ?
            } else if (numberOfElesColored.length === numberOfActiveElementsAllowed) {
                //yes 
                //is the current element an active one ?
                if (currentEle.classList.contains('changeColor')) {
                    //yes 
                    //remove 1 from tracking array
                    numberOfElesColored.pop();
                    //deactivate element
                    return currentEle.classList.remove('changeColor');
                }

            }



        });

    });


})();

你能写下你的意思吗?@Djo我不明白。你想让我解释一下密码吗?嗯。。对不起,我的意思是用户可以选择三个按钮,当用户选择三个按钮时,必须禁用一个按钮