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

后端 未结 2 1339
时光说笑
时光说笑 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

    
    

    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/

提交回复
热议问题