Should href be set with prop() or attr()?

This question already has an answer here:

  • How to change the href for a hyperlink using jQuery 10 answers
  • .prop() vs .attr() 17 answers

  • You would use .attr() , as .prop() is more commonly used for boolean properties such as checked , selected , etc - though it is certainly possible with .prop it's arguably less clear as per your intent

    Though I do believe that ultimately they are very similar (or used to be) functionality-wise

    Just a note: the jQuery API site seems to follow the boolean 'sway':

    .prop() - Examples use checked and disabled

    .attr() - Examples use alt title and src

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

    上一篇: 如何将道具传递给{this.props.children}

    下一篇: 应该用prop()还是attr()来设置href?