javascript – jQuery .prop()返回undefined,而.attr()按预期工作 – 数据 – *

前端之家收集整理的这篇文章主要介绍了javascript – jQuery .prop()返回undefined,而.attr()按预期工作 – 数据 – *前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我只想从两个元素中获取几个属性.从input元素获取属性值按预期工作.问题是从button元素获取属性data-detail属性.它在使用.prop()时返回undefined,但在使用.attr()时按预期工作.

任何人都可以解释我目睹的这种奇怪的行为吗?

HTML

  1. <div class="formRow">
  2. <label for="firstName">First name</label>
  3. <div class="detailsControlBtns">
  4. <button id="editFirstName" class="btn ctaBtn greenBtn editBtn">Edit</button>
  5. <button class="btn ctaBtn greenBtn saveBtn" data-detail="firstName">Save</button>
  6. <button id="closeFirstName" class="btn ctaBtn greyBtn closeBtn">Close</button>
  7. </div>
  8. <input type="text" id="firstName" name="firstName" value="[+firstName+]" readonly>
  9. </div>

JS

  1. $(".saveBtn").on("click",function() {
  2. var saveBtn = $(this);
  3. // The following statement yields undefined. When using .attr() it works as expected.
  4. var detail = saveBtn.prop("data-detail");
  5. var relevantInput = saveBtn.parent().next();
  6. // The following statement works as expected.
  7. var value = relevantInput.prop("value");
  8. // ...
  9. });

解决方法

那是因为HTML元素上没有data-detail属性.

以下是.data(),.prop().attr()快速说明:
DOM元素是一个对象,它具有方法属性(来自DOM)和属性(来自呈现的HTML).其中一些属性通过属性id-> id,class-> className,title-> title,style-> style等获得其初始值.
考虑这个元素:< input type =“checkBox”checked data-detail =“somedata”>
以下结果将是:

  1. $('input').prop('id'); // => " "-empty string,property id exist on the element (defined by DOM),but is not set.
  2. $('input').attr('id');// => undefined - doesn't exist.

如果您执行以下操作:

  1. $('input').attr('id',"someID");
  2. $('input').prop('id'); // => "someID"
  3. $('input').attr('id'); // => "someID"

并且:

  1. $('input').prop('id',"someOtherID");
  2. $('input').prop('id');// => "someOtherID"
  3. $('input').attr('id');// => "someOtherID"

So,some attributes and properties have 1:1 mapping. (change of
the attr result change of the prop and vice versa).

请考虑以下因素:< input type =“text”data-detail =“somedata”value =“someValue”>

  1. $('input').prop('value'); // => "someValue"
  2. $('input').val(); // => "someValue"
  3. $('input').attr('value'); // => "someValue"

如果你这样做:

  1. $('input').prop('value','newVal');
  2.  
  3. // or
  4.  
  5. $('input').val('newVal');
  6.  
  7. $('input').prop('value'); // => "newVal" -value of the property
  8. $('input').val(); // => "newVal" -value of the property
  9. $('input').attr('value'); // => "someValue" -value of the attr didn't change,since in this case it is not 1:1 mapping (change of the prop value doesn't reflect to the attribute value).

案例与@L_301_4@

1)如何获得:

– 请记住,属性名称是data- *,属性名称是数据集,因此:

  1. <input type="checkBox" data-detail="somedata" >
  1. $('input')[0].dataset; //=> [object DOMStringMap] { detail: "somedata"}
  2. $('input')[0].dataset.detail; // => "somedata"
  3. $('input').prop('dataset'); //=>[object DOMStringMap] { detail: "somedata"}
  4. $('input').prop('dataset').detail; // => "somedata"
  5. $('input').data('detail'); // => "somedata"
  6. $('input').attr('data-detail'); // => "somedata"

2)如何设置:

I)$(‘input’).prop(‘dataset’).detail =’newData’;

  1. $('input').prop('dataset'); //=> [object DOMStringMap] { detail: "newData"}
  2. $('input').prop('dataset').detail; // => "newData"
  3. $('input').attr('data-detail'); // => "newData"
  4. $('input').data('detail'); // => "newData"

II)$(‘input’).attr(‘data-detail’,’newData’);

  1. $('input').prop('dataset'); //=> [object DOMStringMap] { detail: "newData"}
  2. $('input').prop('dataset').detail; // => "newData"
  3. $('input').attr('data-detail'); // => "newData"
  4. $('input').data('detail'); // => "newData"

So you can see that here is 1:1 mapping,attr change reflects prop and
vice versa.

但检查第三种方式:

III)$(‘input’).data(‘detail’,’newData’);

  1. $('input').prop('dataset'); // => [object DOMStringMap] { detail: "somedata"}
  2. $('input').prop('dataset').detail; // => "somedata"
  3. $('input').attr('data-detail'); // => "somedata"
  4. $('input').data('detail'); // => "newData" <-----******

那么,这里发生了什么?

$(elem).data(key,value) does not change the HTML5 data-* attributes
of the element. It stores its values in $.cache internally.

因此,为了获取数据 – * .data()永远不会出错:

  1. $(".saveBtn").on("click",function() {
  2. var saveBtn = $(this);
  3. var detail = saveBtn.data("detail");
  4. var relevantInput = saveBtn.parent().next();
  5. var value = relevantInput.prop("value");
  6.  
  7. });

猜你在找的jQuery相关文章