another .attr() vs .prop() question

Possible Duplicate:
.prop() vs .attr()

Given the new .prop() method in jQuery, which is the preferred way to create a new element with particular fields, eg a link:

  • $('<a>').prop('href', '...');
  • $('<a>').attr('href', '...');
  • $('<a href="...">');
  • I've always tended to use #2, but it's unclear whether a new element being put in the DOM shouldn't now use #1 instead.



    I usually would create the a tag like so:

    $('<a>', {href: '...'});
    

    Adds as many attributes to the element at the start. (yes attributes , not properties)

    So you can do the same thing for items like inputs:

          $('<input>',{
                    name: 'some_name',
                    id: 'some_id',
                    value: item_value,
                    readonly: 'true',
                    style: 'background: #eee;',
                    size: 15
                });
    

    Or actual divs:

          $('<div>',{
                    id: 'some_id',
                    text: 'this will be inside the div'
                });
    

    Here is a fiddle example: http://jsfiddle.net/maniator/mbjgd/

    EDIT
    From my comment below:

    The reason for this is because when you create the item, you are creating it by setting it's attributes, later on those attributes become meaningless when they are changed by something like javascript, so at that point you have to use the object's properties to get the real solution that you might be looking for.

    链接地址: http://www.djcxy.com/p/27532.html

    上一篇: 属性与财产

    下一篇: 另一个.attr()与.prop()问题