当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


HTML Style isolation用法及代码示例


DOM样式隔离属性定义元素是否必须创建新的堆栈上下文。

用法:

  • 返回隔离语法:
    object.style.isolation
  • 设置隔离语法:
    object.style.isolation = "auto|isolate|initial|inherit"

属性:


  • auto:它是默认属性值。使用此属性,如果应用于元素的属性之一需要,则仅创建一个新的堆栈上下文。
  • isolate:它指出必须创建一个新的堆栈上下文,而不会出现任何故障。
  • initial:它将隔离属性设置为其默认值。
  • inherit:它继承了父元素的隔离属性值。

返回值:它返回元素的堆叠上下文。

示例1:显示自动属性。

<!DOCTYPE html> 
<html> 
  
<head> 
    <title> 
       HTML | DOM Style isolation Property 
    </title> 
    <center> 
        <h1>Geeks  
          <button onclick="isolation()"> 
            Press 
          </button> 
        </h1> 
  
        <h2> 
          DOM Isolation Property 
        </h2> 
    </center> 
    <h4> 
      Click on the 'Press' button to 
      set the property to 'auto' 
    </h4> 
  
    <style> 
        .p { 
            background-color:green; 
            border:1px solid black; 
        } 
          
        #p1 { 
            width:300px; 
            height:100px; 
        } 
          
        .P { 
            width:100px; 
            height:100px; 
            border:1px solid black; 
            mix-blend-mode:screen; 
        } 
          
        #d { 
            isolation:auto; 
        } 
    </style> 
  
</head> 
  
<body> 
  
    <div id="gfg" class="p"> 
        <div id="d"> 
            <div class="p P"> 
                geeksforgeeks 
            </div> 
  
        </div> 
    </div> 
  
    <script> 
        function isolation() { 
            document.getElementById( 
                "d").style.isolation = "auto"; 
        } 
    </script> 
</body> 
  
</html>

输出:

  • 在单击按钮之前:
  • 单击按钮后:

示例2:显示隔离属性。

<!DOCTYPE html> 
<html> 
  
<head> 
    <title> 
        HTML | DOM Style isolation Property 
    </title> 
    <center> 
        <h1> 
          Geeks  
          <button onclick="isolation()"> 
            Press 
          </button> 
         </h1> 
  
        <h2>DOM Isolation Property</h2> 
    </center> 
    <h4> 
      Click on the 'Press' button to set the  
      property to 'isolate' 
    </h4> 
  
    <style> 
        .p { 
            background-color:green; 
            border:1px solid black; 
        } 
          
        #p1 { 
            width:300px; 
            height:100px; 
        } 
          
        .P { 
            width:100px; 
            height:100px; 
            border:1px solid black; 
            mix-blend-mode:screen; 
        } 
          
        #d { 
            isolation:auto; 
        } 
    </style> 
</head> 
  
<body> 
  
    <div id="gfg" class="p"> 
        <div id="d"> 
            <div class="p P"> 
                geeksforgeeks 
            </div> 
        </div> 
    </div> 
  
    <script> 
        function isolation() { 
            document.getElementById( 
                "d").style.isolation = "isolate"; 
        } 
    </script> 
</body> 
  
</html>

输出:

  • 在单击按钮之前:
  • 单击按钮后:

示例3:显示初始属性。

<!DOCTYPE html> 
<html> 
  
<head> 
    <title> 
        HTML | DOM Style isolation Property 
    </title> 
    <center> 
        <h1> 
         Geeks  
          <button onclick="isolation()"> 
            Press 
          </button> 
        </h1> 
        <h2>DOM Isolation Property</h2> 
    </center> 
    <h4> 
      Click on the 'Press' button to set  
      the property to 'initial' 
    </h4> 
  
    <style> 
        .p { 
            background-color:green; 
            border:1px solid black; 
        } 
          
        #p1 { 
            width:300px; 
            height:100px; 
        } 
          
        .P { 
            width:100px; 
            height:100px; 
            border:1px solid black; 
            mix-blend-mode:screen; 
        } 
          
        #d { 
            isolation:auto; 
        } 
    </style> 
</head> 
  
<body> 
  
    <div id="gfg" class="p"> 
        <div id="d"> 
            <div class="p P"> 
                geeksforgeeks 
            </div> 
        </div> 
    </div> 
  
    <script> 
        function isolation() { 
  
            document.getElementById( 
                "d").style.isolation = "initial"; 
        } 
    </script> 
</body> 
  
</html>

输出:

  • 在单击按钮之前:
  • 单击按钮后:

示例4:显示继承属性。

<!DOCTYPE html> 
<html> 
  
<head> 
    <title> 
        HTML | DOM Style isolation Property 
    </title> 
    <center> 
        <h1> 
          Geeks  
          <button onclick="isolation()"> 
            Press 
          </button> 
  
        </h1> 
  
        <h2> 
          DOM Isolation Property 
        </h2> 
    </center> 
    <h4> 
      Click on the 'Press' button to  
      set the property to 'inherit' 
    </h4> 
  
    <style> 
        .p { 
            background-color:green; 
            border:1px solid black; 
        } 
          
        #p1 { 
            width:300px; 
            height:100px; 
        } 
          
        .P { 
            width:100px; 
            height:100px; 
            border:1px solid black; 
            mix-blend-mode:screen; 
        } 
          
        #d { 
            isolation:auto; 
        } 
    </style> 
</head> 
  
<body> 
  
    <div id="gfg" class="p"> 
        <div id="d"> 
            <div class="p P"> 
                geeksforgeeks 
            </div> 
        </div> 
    </div> 
  
    <script> 
        function isolation() { 
            document.getElementById( 
                "d").style.isolation = "inherit"; 
        } 
    </script> 
</body> 
  
</html>

输出:

  • 在单击按钮之前:
  • 单击按钮后:

注意:Internet Explorer不支持此属性。

浏览器支持:DOM隔离属性支持的浏览器如下:

  • 谷歌浏览器
  • Firefox
  • Opera
  • Safari


相关用法


注:本文由纯净天空筛选整理自riarawal99大神的英文原创作品 HTML | DOM Style isolation Property。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。