简体   繁体   English

如果页面上存在div元素,则隐藏其他元素

[英]if div element is present on page, hide other element

I have a dynamic web form, I'd like to detect if an element is visible; 我有一个动态的Web表单,我想检测一个元素是否可见; and if it is hide another element of mine. 如果它是我的另一个元素,那就藏起来。 I have the below attempt, but this is not working stabilily; 我尝试了以下尝试,但是这种方法无法稳定运行; ie the element isn't always hiding. 即元素并不总是隐藏。 A better technique out there? 有更好的技术吗?

setInterval( myValidateFunction2, 1000);

function myValidateFunction2 () {   
    var inElgbl =  document.getElementById('field_52_116');
    if (typeof(inElgbl) != 'undefined' && inElgbl != null)
    {
        document.getElementById('field_52_24').style.display = "none";
    }   
};  

It is by default display: none; 默认情况下display: none; but may become display: block; 但可能会变成display: block; if it becomes display: block; 如果变为display: block; I would like to display: none; 我想display: none; my other div elem. 我的另一个div元素。

Consider an element to be visible if it consumes space in the document. 如果元素占用了文档中的空间,则认为它是可见的。 For most purposes, this is exactly what you want. 对于大多数目的,这正是您想要的。 Try this: 尝试这个:

setInterval( myValidateFunction2, 1000);

function myValidateFunction2 () {   
    var inElgbl =  document.getElementById('field_52_116');
    if (inElgbl.offsetWidth <= 0 && inElgbl.offsetHeight <= 0)
    {
        document.getElementById('field_52_24').style.display = "none";
    }   
}; 

Probably the most stable way to do this would be using a DOM Mutation Observer and setting it up to watch the document or section of the document that could get the element in question. 可能最稳定的方法是使用DOM Mutation Observer并将其设置为监视可能会引起问题的元素的文档或文档部分。

In the example below, I'll set up an observer to watch an initially empty div and after I've set it up, I'll dynamically add the element we're supposed to be on the lookout for. 在下面的示例中,我将设置一个观察器来观察最初为空的div并在设置好之后,动态添加我们应该关注的元素。 You'll see that the element does not wind up getting displayed. 您会看到该元素没有显示出来。

 // Select the node that will be observed for mutations var targetNode = document.getElementById('parent'); // Options for the observer (which mutations to observe) var config = { attributes: true, childList: true, subtree: true }; // Callback function to execute when mutations are observed function callback(mutationsList, observer) { // We only need to test to see if node is truthy, which it will be if it exists if (document.getElementById('field_52_116')){ document.getElementById('field_52_24').style.display = "none"; console.log("Node detected! Removing...."); } }; // Create an observer instance linked to the callback function var observer = new MutationObserver(callback); // Start observing the target node for configured mutations observer.observe(targetNode, config); // So, we'll add the node to test let newNode = document.createElement("div"); newNode.textContent = "I'm here so the other node should be hidden!"; newNode.id = "field_52_116"; targetNode.appendChild(newNode); // Later, you can stop observing if needed // observer.disconnect(); 
 <div id="parent"></div> <div id='field_52_24'>ELement to hide</div> 

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

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