JQuery document analysis 4-dynamic attribute settings

Source: Internet
Author: User

Example 1: dynamically setting the src attribute of img. Other dom elements are the same.
<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = ISO-8859-1">
<Script type = "text/javascript" src = "js/jquery. js"> </script>
<Script type = "text/javascript">
$ (Document). ready (
Function (){
$ ("# Load"). click (function (){
$ ("Img"). attr ("src", "admin.png"); // set the src attribute of img
$ ("Img"). attr ("src"); // return the src attribute of all img ....
}
);
$ ("# Unload"). click (function (){
$ ("Img"). removeAttr ("src"); // Delete the src attribute of img //
});
}
);
</Script>
<Title> Insert title here </title>
</Head>
<Body>

<Button id = "load"> display </button>
<Button id = "unload"> Delete </button>
</Body>
</Html>
 
2. Differences in the use of html text val ......
Setting ('p'0000.html (); setting ("<a> xxx <a>"); Setting the html content under p corresponds to the innerHTML attribute of dom $ ('P '). text (); set or obtain the text above .. all text under the p tag... or set .... <a> dxx </a> in this column is parsed into text $ ("p "). text ("Hello world! "); $ (" Input "). val (); get or set the value of the form element .. specific filtering can be combined .. the selector selects our element and then obtains or sets the value $ ("input "). val ("hello world! "); 3. Set css attributes by dynamically setting the class of the dom element <Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = ISO-8859-1">
<Script type = "text/javascript" src = "js/jquery. js"> </script>
<Script type = "text/javascript"> www.2cto.com
$ (Document). ready (
Function (){
$ ("# Load"). click (function (){
$ ("P"). addClass ("styleme"); // Add the class attribute styleme
}
); $ ("# Unload"). click (function (){
$ ("P"). removeClass ("styleme"); // uninstall css attributes

});
}
);
</Script>
<Style type = "text/css">
. Styleme {
Font-size: 20px;
Color: blue;
}
</Style>
<Title> Insert title here </title>
</Head>
<Body>
<P> 23232d </p>
<Button id = "load"> display </button> <button id = "unload"> unmount </button>

</Body>
</Html> $ ("p "). toggleClass ("selected"); // indicates adding a class if it does not exist. 4. Set/obtain the attribute value of the first element in the matched element set. $ ("Input [type = 'checkbox']"). prop ("checked"); get the checked attribute value of the first element in the type = checkbox element set in the input element $ ("input [type = 'checkbox']"). prop ("disabled", false); Disable checkbox $ ("input [type = 'checkbox']"). prop ("checked", true); This section describes how to set these attributes in checkbox selection and // jQuery.
 
 

Author: yue7603835

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.