码迷,mamicode.com
首页 > Web开发 > 详细

自定义html的attributes

时间:2016-09-22 23:48:38      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:

本代码来自Coursera的Angular课程,欢迎大家去参加课程.我节选出精华的片段,写到自己博客上.

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>HTML5 Custom Attributes</title>
  </head>
  <body>
    <h1>HTML5 Custom Attributes</h1>

    <div id="target" ng-app="myTestApp">
      <section>Section 1
        <article>Article 1</article>
      </section>
    </div>
    <script>
    // var elem = document.getElementById(‘target‘);
    // console.log(elem);
    //
    // console.log(elem.getAttribute(‘ng-app‘));
    var elem2 = document.querySelector("[ng-app]");
    console.log(elem2);
    console.log(elem2.getAttribute(ng-app));
    </script>
  </body>
</html>

自定义的attribute,html解释器在解释时会自动忽略,因此不会破坏html的运行.

Javascript自带了访问自定义attribute的函数,document.getAttribute(),这可以得到该attribute的值.

此外,还可以通过document.querySelector("[name]")获得有该attribute的dom树的分支.

自定义html的attributes

标签:

原文地址:http://www.cnblogs.com/dagon/p/5898342.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!