What is the difference between removeProp and removeAttr in JQuery 1.6?

后端 未结 3 470
刺人心
刺人心 2020-12-19 02:58

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 th

3条回答
  •  抹茶落季
    2020-12-19 03:46

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

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

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

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

    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.

    EDIT

    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.

    See here for an example: http://jsfiddle.net/tomgrohl/uTCJF/

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

提交回复
热议问题