"Sharp JQuery" learning summary (4) -prop () and ATTR ()

Why can't 80% of the code farmers can't do architects? >>>  hot3.png

When I learned the "checkbox application", I found that the code is running on the FF. I don't think it is a browser. Later, I will change to IE, and I will find it online because it is because of it. Attr () should be replaced to prop () so that it can be displayed normally. The code is as follows:

Is your hobby?
		 <Input Type = "CheckBox" name = "items" value = "football" />
		 <input type = "checkbox" name = "items" value = "Basketball" />
		 <Input Type = "CheckBox" name = "items" value = "badminton" />
		 <Input Type = "Checkbox" name = "items" value = "Table Tennis" />
		 <Input Type = "Button" ID = "Checkedall" Value = "All Choice" />
		 <Input Type = "Button" ID = "CheckedRev" Value = "Reverse Selection" />
		 <Input Type = "Button" ID = "Checkedno" Value = "All Non-selected">
		 <Input Type = "Button" ID = "Send" value = "Submit" />
	</form> 
	<script type="text/javascript">
		$("#checkedAll").click(function(){
			$('[name=items]:checkbox').prop('checked',true);
		 		});//select all 
		$("#checkedNo").click(function(){
			$('[name=items]:checkbox').prop('checked',false);
		 		});//unselect all 
		$("#checkedRev").click(function(){
			$('[name=items]:checkbox').each(function(){
				this.checked=!this.checked;
			})
		 }); // Reverse
		 // replace attr () with prop ();
		$("#send").click(function(){
			 Var str = "You selected: \ r \ n";
			$('[name=items]:checkbox:checked').each(function(){
				str+=$(this).val()+"rn";
			});
			alert(str);
		 }); // Submit the output content
		 // checkbox application
	</script> 

Now why now?

In previous jQuery versions, use attr () to get the properties of the object, such as taking an ALT property of a picture, is $ ("# img"). Attr ('alt'); however, such as: disabled = "disabled" Different in different browsers are different; so from jQuery1.6, use PROP () to get standard properties: true / false; then $ "# checkbox"). Prop ('checked') will only return true / false without returning "disabled" or "";

Principles with prop ():

  1. Only the property name property will take effect immediately, such as SelectedIndex, Tagname, Nodename, Nodetype, OwnerDocument, defaultchecked, and defaultselected;
    2. There is only the property of true / false, that is, the attribute is only the properties of the Boolean value;

Funded in: https: //my.oschina.net/hyzcc/blog/774710

Intelligent Recommendation

jQuery selectors prop() and attr()

Day30 jQuery 1.1.1.1 what isjQuery? n jQueryYesjavaScriptFront frame.Encapsulate common objects and common methods,More convenient to use. It is compatibleCSS3Also compatible with a variety ...

Comparison of .attr() and .prop() in jQuery

Both the attr() and prop() methods can be used to set or return the attribute values ​​of the selected element. Prop() is a new property value after jQuery 1.6. If you are referring to a jQuery.js fil...

Explain attr() and prop() in JQuery

Previously, the operation of the dom attribute is used.attr() This method, only in recent projects, understands that jq is1.6 New versionprop() method. It’s a bit of a slap to look at the defini...

jQuery beauty --attr () and prop ()

  Part Review: jQuery beauty - form object properties Used jQuery partners, some of attr()The method is not strange,attr()Function approach are:Gets the value of a property of the first elem...

JQuery and prop in attr difference of

In a recent written a checkbox effect, and prop in attr use of these methods out of the question. So what are the differences of these two methods. The first thing we think of is to see api, an open a...

More Recommendation

Jquery difference in the prop and attr

Jquery difference in the prop and attr prop: attr prop and attr difference: Reference website:...

JQUERY prop and attr difference

1. The difference between before and after 1-9-1 2. Use   Disclaimer: This article blog original articles, blog without consent shall not be reproduced....

in jQuery attr () and prop () using

For the following reasons: Description: prop () returns true for seleted or false, more intuitive. Reproduced in: https: //my.oschina.net/u/2500703/blog/698790...

The jQuery attr () and prop () method

prop () method Operating inherent properties attr () method Operating custom properties removeProp () method Removal property set by the prop () method removeAttr () method Remove property from a spec...

Use of jquery attr and prop

1. When is prop used in what situations? 2. Pay attention to small details when using prop. 1. Object properties have only two properties, true and false, such as checked, selected or disabled, use pr...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top