关于Js对页面进行渲染

先做一个测试代码
当前代码测试 页面中虽然会出现三个input标签
但是第三个无法被js进行渲染
也就是说input内所输出的内容并不是红色的而是默认的黑色

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
   <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
   <title>Document</title>
   <script src="http://libs.baidu.com/jquery/1.8.3/jquery.min.js"></script>
</head>
<body>
   <input type="text" class="fuck" />
   <input type="text" class="fuck" />
   <script>
       $('fuck').css('color', 'red');
       var str =$("<input class=\"fuck\">");
       $('body').append(str);
   </script>
</body>
</html>

而稍微切换下js的顺序
input就可以正常渲染

   <script>
       var str =$("<input class=\"fuck\">");
       $('body').append(str);
       $('fuck').css('color', 'red');
   </script>

总结:

页面添加的内容要进行渲染,就一定要在渲染的js之前插入所需要进行渲染的内容
ajxa或datatable进行渲染的页面添加需要js渲染的代码时
需要通过回调函数对页面进行二次渲染

代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
   <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
   <title>Document</title>
   <script src="http://libs.baidu.com/jquery/1.8.3/jquery.min.js"></script>
</head>
<body>
   <input type="text" class="fuck" />
   <input type="text" class="fuck" />

   <script>
       init($('.fuck'));
       function init(obj){
           obj.css('color', 'red');
       }//渲染页面中的boj(对象)的color

       var str =$("<input class=\"fuck\">");  
       $('body').append(str);//   对页面进行添加一个html
       init(str);//进行重新渲染


   </script>
</body>
</html>

涉及理解:异域(2015.12.30)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 问答题47 /72 常见浏览器兼容性问题与解决方案? 参考答案 (1)浏览器兼容问题一:不同浏览器的标签默认的外补...
    _Yfling阅读 13,790评论 1 92
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,854评论 18 139
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 172,976评论 25 708
  • 01 毕业后的迷茫 12年前我经济法专业本科毕业,对未来职业没有规划,不知道该做什么,于是发挥“会考试”(注意不是...
    资深美丽拆书家任丽娜阅读 1,361评论 10 11
  • 去年11月去给瑶做伴娘时,就想要写篇作文,给我互相陪伴了十三年的两位姑娘。时至今日,瑶的女儿小桃子已经出生21天,...
    羊它它阅读 551评论 0 1