attributes VS properties

Attributes are defined by HTML. Properties are defined by DOM.

  • Property属于面向对象理论范畴。在使用面向对象思想编程的时候常常需要对客观事物进行抽象,再把抽象出来的结果封装成类,类中用来表示对象状态的成员就是property。

  • Attribute则是编程语言文法层面的东西,比如有两个同类的语法元素A和B,为了区分A和B,就需要加一些Attribute,attribute可以翻译为特征。

    var a1 = $("#img1").attr("src");//pic.jpg
    var a2 = $("#img1").attr("alt");//imgAlt
    console.log(a1);
    console.log(a2);

    var a1 = $("#img1").prop("src");//http://localhost:63342/nodejs/pic.jpg
    var a2 = $("#img1").prop("alt");//imgAlt
    console.log(a1);
    console.log(a2);
  //小结:.attr("src")获取到html中的属性,.prop("src")获取到绝对路径。

==>

  • attr 是在HTML代码中的东西(代码中是什么就是什么)
  • prop可以看作是利用js代码取得该DOM元素的信息

你可能感兴趣的:(attributes VS properties)