.prop() VS .val() .Setting an input text value with jQuery

后端 未结 6 1582
栀梦
栀梦 2021-01-05 12:42

After reading (the interesting) .prop() vs .attr() and jQuery Performance : attributes doubt arise in my mind about what is better to use: .prop() or .val() ? I want to set

6条回答
  •  孤城傲影
    2021-01-05 13:04

    The other answers explain the main differences pretty well with one exception.

    Since jQuery "normalizes" browser differences val() removes carriage returns.

    This affects textareas in IE 8 and below.

    Example: For the textarea input of

    HEL
    
    
    LO
    

    .val() gives you the string "HEL\n\nLO" and a length of 7

    .prop("value") gives you the string "HEL\r\n\r\nLO" with a length of 9

提交回复
热议问题