How safe/reliable/cross-browser compatible is $(this)[0].defaultValue

后端 未结 2 1337
时光说笑
时光说笑 2020-12-06 19:09

I would appreciate some feedback regarding the use of $(this)[0].defaultValue for determining whether a textbox value has change from the original value e.g.



        
相关标签:
2条回答
  • 2020-12-06 19:39

    .defaultValue is non-standard and therefore not mentioned in any W3C draft / specifications. If you're in charge of the HTML markup being rendered, the HTML5 data- attributes seem to be a very convenient way to get the desired behavior cross-browser.

    So you could render it like

    <input type="text" value="foo" data-default="foo" />
    

    jQuery will grab those data- attributes and store the part behind - as key in its data expand object for that specific node. That means you can just access those values by calling

    $('input').data('default') // === 'foo'
    

    Example: http://jsfiddle.net/zAuPf/

    0 讨论(0)
  • 2020-12-06 19:40

    It seems pretty safe. i just used this fiddle: http://jsfiddle.net/rXsrQ/ and tested it across chrome/firefox 3.6/safari 4/ ie8, they all behave the same.

    It is described here: https://developer.mozilla.org/en/DOM/HTMLTextAreaElement and there is no notice about compatibility issues.

    It is also described here: http://msdn.microsoft.com/en-us/library/ms533718(v=vs.85).aspx and seems to be compatible with ie 5 and up, so i don't seem why it shouldn't be safe to use.

    0 讨论(0)
提交回复
热议问题