Javascript innerHTML.indexOf()CSS控件

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Javascript innerHTML.indexOf()CSS控件相关的知识,希望对你有一定的参考价值。

好的,所以这里是我为客户写的一些javascript。它的目的是根据星期几来控制首先显示哪个元素。但是,我有一个问题,如果特定日期没有可用的内容,该元素将返回字符串“未找到任何帖子”。

代码的最后几行获取元素的位置,我的意图是使用它来隐藏父容器。任何建议将不胜感激!

var d = new Date();
var dayInt = d.getDay();
var slides = document.getElementsByClassName("eventDisplay");
var showInt = 7;
var counter = 0;
console.log('started: ' + showInt);
console.log(slides.length);
for(var i = 0; i < slides.length; i++)
{
    console.log('loop');
    if(i == dayInt || (showInt < 7 && counter < 7)){ //if equal OR if showInt has been changed
        console.log('true'); 
        if(showInt > 0){ //and if not showing more than 7
            slides[i].style.display = 'none';
            showInt = showInt - 1;
            counter = counter + 1;
            console.log('reached: ' + showInt);
        }
    }
}

var position = document.documentElement.innerhtml.indexOf('Not found any post');
console.log(position);
position.innerHTML = '';

安慰:

started: 7
14
loop
true
reached: 6
loop
true
reached: 5
loop
true
reached: 4
loop
true
reached: 3
loop
true
reached: 2
loop
true
reached: 1
loop
true
reached: 0
loop
23837
答案

解决方案:检查您正在显示的组件的innerHTML。不知道我为什么不早点想到这个。

var d = new Date();
var dayInt = d.getDay();
var slides = document.getElementsByClassName("eventDisplay");
var showInt = 7;
var counter = 0;
console.log('started: ' + showInt);
console.log(slides.length);
for(var i = 0; i < slides.length; i++)
{
    console.log('loop');
    if(i == dayInt || (showInt < 7 && counter < 7)){ //if equal OR if showInt has been changed
        console.log('true'); 
        if(showInt > 0){ //and if not showing more than 7
            if(slides[i].innerHTML.indexOf('Not found any post') > 0){
                //do nothing
            } else { 
                slides[i].style.display = 'block';
            }

            showInt = showInt - 1;
            counter = counter + 1;
            console.log('reached: ' + showInt);
        }
    }
}

以上是关于Javascript innerHTML.indexOf()CSS控件的主要内容,如果未能解决你的问题,请参考以下文章

javascript JavaScript isset()等效: - JavaScript

JavaScript 使用JavaScript更改CSS(JavaScript)

JavaScript之基础-1 JavaScript(概述基础语法)

前端基础-JavaScript的基本概述和语法

JavaScript

JavaScript