Should href be set with prop() or attr()? [duplicate]

为君一笑 提交于 2019-12-18 18:36:43

问题


If I want to change the href of a link dynamically, should I do so using prop() or attr()?


回答1:


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



来源:https://stackoverflow.com/questions/18189858/should-href-be-set-with-prop-or-attr

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!