简体   繁体   English

除非有时将元素悬停在inspect内,否则有时会用Javascript加载HTML内容

[英]Javascript to load content in HTML sometimes unless element hovered inside inspect

I have made a widget. 我做了一个小部件。 The content of this is loaded dynamically by JS. 其内容由JS动态加载。 However, sometimes when a page loads the data does not show up on the widget unless it is clicked upon. 但是,有时在页面加载时,除非单击数据,否则数据不会显示在窗口小部件上。 Even going into inspect and hovering over any element loads the items. 即使进入检查并将鼠标悬停在任何元素上也会加载项目。 The two images below are before and after click. 下面的两个图像是单击前后的。

Update 1 - Click doesn't necessarily loads the data but hovering inside inspect element does. 更新1-单击不一定加载数据,但是将鼠标悬停在inspect元素内部。

Update 2 - This seem to get fixed if I call the function to put data multiple times. 更新2-如果多次调用该函数放置数据,此问题似乎已解决。 Note: I have 15 sec timer to call the function, however if I reduce the interval of first 4 runs, it fixes. 注意:我有15秒的计时器来调用该函数,但是,如果我减小了前4次运行的间隔,它会修复。 However, this seems like a workaround and not a solution. 但是,这似乎是一种解决方法,而不是解决方案。

Update 3 - As Suggested, this appears more of a css issue Created a Fiddle 更新3-如建议的那样,这似乎更多是css问题创建的小提琴

单击/悬停之前 单击/悬停后

Here is the function that is putting the data 这是放置数据的功能

function putData(dataSet) {
if(dataSet.length == 3) {
    document.getElementById('liveBlock').style.display = 'none';
    document.getElementById('upcomingCompletedBlock').style.display = 'block';
    for(var i=1; i<=3; i++) {
        title = dataSet[i-1].title;
        teams = title.split(' vs ');
        homeTeamName = teams[0];
        awayTeamName = teams[1];
        homeTeam = dataSet[i-1].homeTeamLogoUrl;
        awayTeam = dataSet[i-1].awayTeamLogoUrl;
        document.getElementById('botCupLogo'+i+'1').src=homeTeam;
        document.getElementById('botCupLogo'+i+'2').src=awayTeam;
        document.getElementById('botCupName'+i+'1').innerHTML=homeTeamName;
        document.getElementById('botCupName'+i+'2').innerHTML=awayTeamName;

        if(dataSet[i-1].matchStatus == 'UPCOMING') {
            var date = new Date(dataSet[i-1].startDateTime);
            document.getElementById('summaryText'+i).innerHTML = date;
        }
        else if(dataSet[i-1].matchStatus == 'COMPLETED') {
            document.getElementById('summaryText'+i).innerHTML = dataSet[i-1].summaryText;
        }
    }
}

It is called by a different function which fetches data from an API. 它由另一个函数调用,该函数从API提取数据。 The data is correctly fetched and passed into this function 数据已正确获取并传递到此函数

I have made a widget. 我做了一个小部件。 The content of this is loaded dynamically by JS. 其内容由JS动态加载。

Do you mean you add the HTML content by JS? 您是说要通过JS添加HTML内容吗? I bet that there is a coordination issue. 我敢打赌,这里存在协调问题。 Maybe the function that adds this content, call it createContent , fires one or more times after your putData function, and overwrites the DOM elements to their initial values. 也许添加此内容的函数称为createContent ,在putData函数之后触发一次或多次,然后将DOM元素覆盖为其初始值。 Or maybe putData fires early and the elements are not loaded. 或者也许putData会提前触发并且元素不会被加载。

This seem to get fixed if I call the function to put data multiple times. 如果我多次调用该函数来放置数据,则此问题似乎已解决。

Which supports the coordination issue hypothesis. 支持协调问题假设。

Advices 忠告

  • Wrap your putData function content in a try catch . putData函数的内容包装在try catch It will prevent your JS from crash and will help you to debugging. 这将防止您的JS崩溃并帮助您进行调试。

    Since you are manipulating DOM elements that could not be loaded or targeted. 由于您正在处理无法加载或定位的DOM元素。 Something like this: 像这样:

function putData(dataSet) {
    try {
        // add your code here
        return true;
    } catch (reason) {
        console.error(reason); // <-- pay attention to the console
        return false;
    }
}
  • Do not call the putData multiple times ramdomly. 不要putData多次调用putData Instead, call it when the HTML content is ready. 而是在HTML内容准备就绪时调用它。

    For example, at the end of createContent you can call the function that fetches the data and then putData with its response. 例如,在createContent的末尾,您可以调用获取数据的函数,然后putData及其响应。 Another option is to use a custom event and dispatch it at the end of createContent . 另一种选择是使用自定义事件,并将其分派到createContent的末尾。

  • Inside the proposed function createContent , check if content is alreadty loaded. 在建议的函数createContent ,检查是否已加载内容。 And if isLoaded , do not load it again. 如果isLoaded ,请不要再次加载它。

Hope it helps. 希望能帮助到你。

Here you go: https://jsfiddle.net/zqxa0fL8/ 您在这里: https : //jsfiddle.net/zqxa0fL8/

What I had to do is to remove the unnecessary perspective(1200px) from all CSS rules except mybot .cube 我要做的是从mybot .cube之外的所有CSS规则中删除不必要的perspective(1200px)

The red side still has an issue, as it's CSS differs from the other sides. 红色方面仍然存在问题,因为它的CSS与其他方面不同。 Should be fine after you do a refactoring. 进行重构后应该没问题。

Too much CSS stuff in your code. 您的代码中的CSS内容过多。 Try minimize the code only to what is needed. 尝试仅将代码最小化为所需的代码。

UPDATE: 更新:

See: https://jsfiddle.net/8u24pyeq/ Turns out the issue is because of backface-visibility . 参见: https: //jsfiddle.net/8u24pyeq/原来,该问题是由于backface-visibility You need to wait before page renders and only then apply the setting. 您需要等待页面呈现之前,然后再应用设置。

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

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