简体   繁体   English

JavaScript样式(如果未满足声明)

[英]javascript style if statement not being met

I'm having toruble with this function, it requires two clicks before the if statement is satisfied even though in the CSS the condition should be met. 我在使用此功能时遇到麻烦,即使需要在CSS中满足条件,在if语句得到满足之前也需要单击两次。 On the fist click, the console shows triggered but not if state on the second click it does show if state can anyone understand why the condition is not being met? 第一次单击时,控制台将显示已triggeredif state第二次单击,则不会显示if state但会显示if state有人可以理解为什么不满足条件?

function searchShow() {
    console.log('started');
    document.getElementById('top_line_2a').addEventListener('click', function() {
      console.log('triggered')
      var searchClickIcon = document.getElementById('top_line_2a');
      var searchClick = document.getElementById('top_line_3');
      if(searchClick.style.height == '0em') {
        console.log('if state');
        //searchClick.style.display = 'block';
        searchClick.style.height = '3em';
        searchClickIcon.style.color = 'white';
        searchClickIcon.style.textShadow = '0px 0px 7px white';
        document.getElementsByClassName('search')[0].focus();
      } else {
        //searchClick.style.display = 'none';
        searchClick.style.height = '0em';
        searchClickIcon.style.color = 'rgba(255, 187, 61, 1)';
        searchClickIcon.style.textShadow = '';
      }
    })
    console.log('added');
  }

When implementing ping-pong / toggle effects, try not to compare with attribute value directly. 在实现乒乓/切换效果时,请不要直接与属性值进行比较。 The zero height could be "0em", "0", or numeric 0. You could try normalizing the value for this one particular case: 零高度可以是“ 0em”,“ 0”或数字0。您可以尝试针对这种特殊情况将值标准化:

if (parseInt(searchClick.style.height,10)==0) {
  // show the container
} else {
  // hide the container
}

A much more reliable way is to take advantage of the fact that every DOM element can be dynamically assigned new attributes. 一种更可靠的方法是利用可以为每个DOM元素动态分配新属性的事实。 Since you already have a handle to the searchClick object: 由于您已经具有searchClick对象的句柄:

if (searchClick.showing){
  searchClick.showing=null;
  // hide the container
} else {
  searchClick.showing=true;
  // show the container
}

"showing" is your own attribute. “显示”是您自己的属性。 When you first click on it, the marker is not there, so it'll show the container (initially hidden). 当您第一次单击它时,标记不存在,因此它将显示容器(最初是隐藏的)。 Then the showing flag is attached to it, so you can detect it in the next click. 然后将显示标志附加到它,以便您可以在下次单击中检测到它。 If your initial state is showing, then use a different flag to reverse the logic. 如果显示的是初始状态,请使用其他标志反转逻辑。 This is a sure fire method to implement a toggle. 这是实现切换的肯定触发方法。

You shouldn't be using the height. 您不应该使用高度。 Use a variable instead. 请改用变量。

var triggered = false;
function searchShow() {
  document.getElementById('top_line_2a').addEventListener('click', function() {
    //Do stuff
    if(!triggered) {
      triggered = true;
      //Do stuff
    } else {
      triggered = false;
      //Do stuff
    }
  })
}

Checking for a style in an if statement isn't a good practice. 在if语句中检查样式不是一个好习惯。 If you ever change the size of your container for X reason, you'll also have to change the if/else statement to fit the change. 如果您由于X原因更改了容器的大小,则还必须更改if / else语句以适应更改。 It also make the code that much less clear to whoever will read it. 这也使代码的可读性大大降低。 Always try to avoid using hardcoded numbers when you can use something more effective. 当您可以使用更有效的方法时,请始终避免使用硬编码数字。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM