.prop()与.attr()

JavaScript

逆天L

2020-03-09

因此jQuery 1.6具有新功能prop()

$(selector).click(function(){
    //instead of:
    this.getAttribute('style');
    //do i use:
    $(this).prop('style');
    //or:
    $(this).attr('style');
})

还是在这种情况下他们做同样的事情?

如果我确实必须切换到using prop()attr()那么如果我切换到1.6 ,所有旧呼叫都会中断?

更新

selector = '#id'

$(selector).click(function() {
    //instead of:
    var getAtt = this.getAttribute('style');
    //do i use:
    var thisProp = $(this).prop('style');
    //or:
    var thisAttr = $(this).attr('style');

    console.log(getAtt, thisProp, thisAttr);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.0/jquery.min.js"></script>
<div id='id' style="color: red;background: orange;">test</div>

(另请参见此提琴:http : //jsfiddle.net/maniator/JpUF2/

控制台将记录getAttribute为字符串,将记录attr为字符串,但是将记录propCSSStyleDeclaration,为什么?将来如何影响我的编码?

第196篇《.prop()与.attr()》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

9个回答
小卤蛋前端 2020.03.09

Gary Hole answer is very relevant to solve the problem if the code is written in such way

obj.prop("style","border:1px red solid;")

Since the prop function return CSSStyleDeclaration object, above code will not working properly in some browser(tested with IE8 with Chrome Frame Plugin in my case).

Thus changing it into following code

obj.prop("style").cssText = "border:1px red solid;"

solved the problem.

逆天 2020.03.09

1) A property is in the DOM; an attribute is in the HTML that is parsed into the DOM.

2) $( elem ).attr( "checked" ) (1.6.1+) "checked" (String) Will change with checkbox state

3) $( elem ).attr( "checked" ) (pre-1.6) true (Boolean) Changed with checkbox state

  • Mostly we want to use for DOM object rather then custom attribute like data-img, data-xyz.

  • Also some of difference when accessing checkbox value and href with attr() and prop() as thing change with DOM output with prop() as full link from origin and Boolean value for checkbox (pre-1.6)

  • We can only access DOM elements with prop other then it gives undefined

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.0/jquery.min.js"></script>
<!doctype html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <title>prop demo</title>
  <style>
    p {
      margin: 20px 0 0;
    }
    b {
      color: blue;
    }
  </style>

</head>

<body>

  <input id="check1" type="checkbox" checked="checked">
  <label for="check1">Check me</label>
  <p></p>

  <script>
    $("input").change(function() {
      var $input = $(this);
      $("p").html(
        ".attr( \"checked\" ): <b>" + $input.attr("checked") + "</b><br>" +
        ".prop( \"checked\" ): <b>" + $input.prop("checked") + "</b><br>" +
        ".is( \":checked\" ): <b>" + $input.is(":checked")) + "</b>";
    }).change();
  </script>

</body>

</html>

达蒙理查德 2020.03.09

在jQuery 1.6之前,该attr()方法有时在检索属性时考虑属性值,这会导致行为不一致。

prop()方法的引入提供了一种在检索属性的同时显式检索属性值方法.attr()

文件:

jQuery.attr() 获取匹配元素集中第一个元素的属性值。

jQuery.prop() 获取匹配元素集中第一个元素的属性值。

逆天路易 2020.03.09

attributes -> HTML

properties -> DOM

达蒙阿飞斯丁 2020.03.09

通常,您需要使用属性。仅将属性用于:

  1. 获取自定义HTML属性(因为它未与DOM属性同步)。
  2. 获取与DOM属性不同步的HTML属性,例如,获取标准HTML属性的“原始值”,例如 <input value="abc">.
Harry小卤蛋 2020.03.09

.attr()

  • 获取匹配元素集中第一个元素属性
  • 给您元素的值,就像在页面加载时在html中定义的那样

.prop()

  • 获取匹配元素集中第一个元素属性
  • 给出通过javascript / jquery修改的元素的更新值
猿GO 2020.03.09

全部都在文档中

属性和属性之间的差异在特定情况下可能很重要。在jQuery 1.6之前,.attr()方法在检索某些属性时有时会考虑属性值,这可能会导致行为不一致。从jQuery 1.6开始,.prop()方法提供了一种显式检索属性值的方法,而.attr()则检索属性。

所以使用道具!

老丝村村 2020.03.09

TL; DR

使用prop()attr()在大多数情况下。

属性是输入元件的当前状态。一个属性是默认值。

一个属性可以包含不同类型的事物。属性只能包含字符串

乐逆天 2020.03.09

属性在DOM中;HTML中的一个属性被解析为DOM。

更多细节

如果更改属性,则更改将反映在DOM中(有时使用不同的名称)。

示例:更改class标签属性将更改classNameDOM中该标签属性。如果标签上没有属性,则仍然具有相应的DOM属性,该属性为空或默认值。

示例:虽然您的标签没有class属性,但是DOM属性className确实存在,并且具有空字符串值。

编辑

如果更改一个,则另一个将由控制器更改,反之亦然。该控制器不在jQuery中,而是在浏览器的本机代码中。

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android