首页 \ 问答 \ 在DOMContentLoaded触发后应用样式?(Are styles applied after DOMContentLoaded is triggered?)

在DOMContentLoaded触发后应用样式?(Are styles applied after DOMContentLoaded is triggered?)

考虑下面的HTML文档:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style type="text/css">
        body {
            background: crimson;
        }

        div {
            transition: opacity 5s;
            font-size: 4em;
            opacity: 0;
        }

        .loaded div {
            opacity: 1;
        }
    </style>
    <script type="text/javascript">
        document.addEventListener('DOMContentLoaded', function() {
            document.getElementById('body').className += "loaded";
        })
    </script>
</head>
<body id="body">
<div>
TEST
</div>
</body>
</html>

该div应该将其不透明度设置为0,并将不透明度设置为5秒。

当DOM被加载时,body被赋予一个将div不透明度设置为1的类。

我期待div不透明度从5秒过渡到1。 但由于某种原因,它会立即发生。

如果我使用setTimemout,每个按预期工作:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style type="text/css">
        body {
            background: crimson;
        }

        div {
            transition: opacity 5s;
            font-size: 4em;
            opacity: 0;
        }

        .loaded div {
            opacity: 1;
        }
    </style>
    <script type="text/javascript">
        document.addEventListener('DOMContentLoaded', function() {
            setTimeout(function() {
                document.getElementById('body').className += "loaded";
            }, 0);
        })
    </script>
</head>
<body id="body">
<div>
TEST
</div>
</body>
</html>

让我感到惊讶的是DOMContentLoaded事件触发后,样式被加载。 这是正常行为还是我在这里做错了?


Consider the following HTML document:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style type="text/css">
        body {
            background: crimson;
        }

        div {
            transition: opacity 5s;
            font-size: 4em;
            opacity: 0;
        }

        .loaded div {
            opacity: 1;
        }
    </style>
    <script type="text/javascript">
        document.addEventListener('DOMContentLoaded', function() {
            document.getElementById('body').className += "loaded";
        })
    </script>
</head>
<body id="body">
<div>
TEST
</div>
</body>
</html>

The div is supposed to have its opacity set to 0 and a 5s transition on opacity.

When the DOM is loaded, the body is given a class that set the div opacity to 1.

I'm expecting the div opacity to transition from 0 to 1 in 5s. But for some reason, it happens immediately.

If I use setTimemout, every works as expected:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style type="text/css">
        body {
            background: crimson;
        }

        div {
            transition: opacity 5s;
            font-size: 4em;
            opacity: 0;
        }

        .loaded div {
            opacity: 1;
        }
    </style>
    <script type="text/javascript">
        document.addEventListener('DOMContentLoaded', function() {
            setTimeout(function() {
                document.getElementById('body').className += "loaded";
            }, 0);
        })
    </script>
</head>
<body id="body">
<div>
TEST
</div>
</body>
</html>

Makes me wonder is styles are loaded after DOMContentLoaded event is triggered. Is this a normal behavior or am I doing something wrong here ?


原文:https://stackoverflow.com/questions/46989755
更新时间:2022-03-28 19:03

最满意答案

不固定的星号
var reg=/^(.{2})(.*)(.{2})$/;
var str="sadf[.,saj#ofief";
str.replace(reg,"$1****$2");

///////////////////////////////////////////

固定的星号
var reg=/^(.{2})(.*)(.{2})$/;
var str="sadf[.,saj#ofief";
str=str.replace(reg,function(a,b,c,d){
    return b+c.replace(/./g,"*")+d;
});
console.log(str);

其他回答

<script language="javascript" type="text/javascript">      var str="did in (&100005&,&30001&,&30002&)";      for (var i=0;i<str.length;i++)      {          str=str.replace("&","'");      }      alert(str);  &lt;&#47;script&gt;

相关问答

更多
  • ^[a-zA-Z].{3}\. ^ 开头 [a-zA-Z] 一位大小写字母 .{3} 三位任意字符 \. 字符'.'
  • 你的正则没有错...但这属于 Dynamic Regex Replacement 了 python 我是不太会,但你要用正则在查询替代的同时执行code,这个恐怕只有 perl 能够做到... 1. perl -le '$ARGV[0]=~s/(? 评论0 0 0 加载更多
  • §(\d+)<\/p> 替换成 §\1<\/h1> 或者 §$1<\/h1>
  • 是可以的 比如“.*?name.*?”就可以匹配找出所有含有"name"的字符串,就像mysql查询用"where .. like '%name%'"一样的。并且正则可以更灵活,如果你写的".*?name\d{2}.*?",那么能找出像含有"name12","name09"这种name后至少跟2位数字的。总之,正则可以实现模糊查询,不过如果你的需求不是这么灵活匹配,那么就用普通字符串处理函数就可以了。
  • mysql的正则匹配用regexp,而替换字符串用REPLACE(str,from_str,to_str) 举例如下: UPDATE myTable SET HTML=REPLACE(HTML,' ','') WHERE HTML REGEXP '( \s*){2,}' 达到的效果:会把所有 全部替换掉。 mysql中常用的替换函数 所用到的函数: locate: LOCATE(substr,str) POSITION(substr IN str) 返回子串 substr 在字符串 str 中第一次出现的位 ...
  • 请参考下面程序: import java.util.LinkedHashMap; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; public class t { public static void main(String[] args) { String regExp = "(\\{.*?\\})"; String str = "{1,1,2} + {3,2,1} + {$1}"; ...
  • import java.util.regex.Matcher; import java.util.regex.Pattern; public class TestRegex { public static void main( String[] args ) { String regex = "第[0-9]*条"; String str = "第9条,数据错误,错误信息,第jjj哦条哦条我的条件如何?第221条xx"; Pattern pat = Pattern.compile(regex); Matche ...
  • str是你自己的字符串自己测试
  • 正则表达式 \{\{(.*?)\}\} 取第一捕获组的数据 我给你一个Javascript语言的例子,你看看吧. var str=' 课题:{{课题}} 子课题:{{子课题_A}}, {{子课题_B}} Email:{{Email_1}}, {{Email_2}} '; var regex=/\{\{(.*?)\}\}/g; var result; while((result = regex.exec(str)) != null){ document.write(result[1]+" "); }
  • 不固定的星号 var reg=/^(.{2})(.*)(.{2})$/; var str="sadf[.,saj#ofief"; str.replace(reg,"$1****$2"); /////////////////////////////////////////// 固定的星号 var reg=/^(.{2})(.*)(.{2})$/; var str="sadf[.,saj#ofief"; str=str.replace(reg,function(a,b,c,d){ return b+c.repl ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)