href 应该用 prop() 还是 attr() 设置? [英] Should href be set with prop() or attr()?

查看:35
本文介绍了href 应该用 prop() 还是 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() 更常用于布尔值checkedselected 等属性 - 尽管 .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() - 示例使用 checkeddisabled

.attr() - 示例使用 alt titlesrc

这篇关于href 应该用 prop() 还是 attr() 设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆