href 应该用 prop() 还是 attr() 设置? [英] Should href be set with prop() or attr()?
问题描述
如果我想动态地改变一个链接的href,我应该使用prop()
还是attr()
?
If I want to change the href of a link dynamically, should I do so using prop()
or attr()
?
推荐答案
您可以使用 .attr()
,因为 .prop()
更常用于布尔值checked
、selected
等属性 - 尽管 .prop
肯定是可能的,但根据您的意图可能不太清楚
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
请注意:jQuery API 站点似乎遵循布尔摇摆":
Just a note: the jQuery API site seems to follow the boolean 'sway':
.prop()
- 示例使用 checked
和 disabled
.attr()
- 示例使用 alt
title
和 src
这篇关于href 应该用 prop() 还是 attr() 设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!