JQuery 1.6中removeProp和removeAttr有什么区别? [英] What is the difference between removeProp and removeAttr in JQuery 1.6?

查看:100
本文介绍了JQuery 1.6中removeProp和removeAttr有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果你删除某些东西,你应该使用removeAttr,它会无声地失败吗?它会起作用吗?它实际上会删除整个属性还是仅删除其中的值?

If you removeProp on something you should have used removeAttr on will it silently fail? Will it work? Will it actually remove the entire attribute or just the value inside it?

如果使用prop()添加'checked',是否可以使用removeAttr删除它?

If 'checked' is added using prop(), can it be removed with removeAttr?

很多问题!

推荐答案

元素的属性是某种东西喜欢'上课'。而它的属性将是'className'。

An attribute of an element is something like 'class'. Whereas its property would be 'className'.

这是将jQuery.prop和jQuery.propHooks添加到1.6版本中的原因,以便更容易使用它们。

This is the reason for adding jQuery.prop and jQuery.propHooks into version 1.6, to make it easier working with both.

因此,如果该属性与该属性具有相同的名称,则可以同时使用removeProp或removeAttr。

So if the the property had the same name as the attribute you could use both removeProp or removeAttr.

I在jQuery论坛上问了一个类似的问题,得到了这个答案:

I asked a similar question on jQuery forum, got this answer:


是的,attr是指html属性
,因为它们是严格的定义。道具是物业的
。例如,假设
你有一个节点elem,类
something(raw元素不是jQuery
object)。 elem.className是
属性,但是
属性所在的位置。更改类
属性也会自动更改属性
,反之亦然。
目前,attr是混乱而且
令人困惑,因为它已经尝试了这两个函数的
工作,因此有很多错误
。 jQuery.fn.prop的
介绍将
解决几个阻塞,单独的代码
因为它应该从
开始分开,并给开发人员
更快的函数去做那些他们期望他们做的事情。让我在一秒内弥补
的百分比,并说我从
我支持IRC的经验和
读取其他代码,95%的attr使用
的情况不会必须将
换成道具。

Yes, attr is meant for html attributes as they are strictly defined. prop is for properties. So for instance, say you have a node elem with class "something" (raw element not jQuery object). elem.className is the property, but is where the attribute resides. Changing the class attribute also changes the property automatically and vise versa. Currently, attr is jumbled and confusing because it has tried to the job of both functions and there are many bugs because of that. The introduction of jQuery.fn.prop will solve several blockers, separate code as it should have been separated from the beginning, and give developers faster functions to do what they expect them to do. Let me make up a percentage for a sec and say that from my experience in the support IRC and reading other's code, 95% of the use cases for attr will not have to switch to prop.

编辑

最好坚持使用jQuery.attr或jQuery.prop。当使用两者设置和删除checked属性时,似乎有些奇怪的行为。

It may be best to stick to using either jQuery.attr or jQuery.prop. Theres seems to be some strange behaviour when setting and removing the checked attribute using both.

请看这里的示例: http://jsfiddle.net/tomgrohl/uTCJF/

1.6中有一个错误选择: http://bugs.jquery.com/ticket/9079

There is a bug in 1.6 to do with selected: http://bugs.jquery.com/ticket/9079

这篇关于JQuery 1.6中removeProp和removeAttr有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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