JQuery implements P tag operations

Source: Internet
Author: User

JQuery implements P tag operations

Note: The following code has passed the W3School online test.

Method for retrieving the element itself (Object) based on the id:

$ (# Id );

For example:

 

<Script src =/jquery/jquery-1.11.1.min.js> </script> <script> $ (document ). ready (function () {$ (p ). click (function () {alert ($ (# p1) ;}); </script> 

If you click me, I will disappear.

Click me and I will disappear.

Click me.


Running result:

 

 

Method for retrieving the element itself (Object) based on the class:

 

<Script src =/jquery/jquery-1.11.1.min.js> </script> <script> $ (document ). ready (function () {$ (p ). click (function () {alert (obtains the element itself based on the class: + $ (. class1) ;};}); </script> 

If you click me, I will disappear.

Click me and I will disappear.

Click me.


Running result:

 

 

To obtain the P tag text content, follow these steps:

Method 1: $ (# p1). text ();

Method 2: Certificate (#p1).html ();

For example:

 

<Script src =/jquery/jquery-1.11.1.min.js> </script> <script> $ (document ). ready (function () {$ (p ). click (function () {alert (text () method: + $ (# p1 ). text (); alert (html () method: Plain plain (#p1).html () ;}); </script> 

If you click me, I will disappear.

Click me and I will disappear.

Click me.


Running result:

 

$ (# P1). text (); method:

 

((#P1).html (); method:

 

How to Get attributes:

 

$(this).attr(id)
For example:

 

 

<Script src =/jquery/jquery-1.11.1.min.js> </script> <script> $ (document ). ready (function () {$ (p ). click (function () {alert (name attribute value in the P Tag: + $ (this ). attr (name); alert (class Attribute Value in the P Tag: + $ (this ). attr (class); alert (id attribute value in the P Tag: + $ (this ). attr (id) ;}); </script> 

If you click me, I will disappear.

Click me and I will disappear.

Click me.


Running result:

 

Get the name property:

 

Get class attributes:

 

Get id attributes:

 

 

 

 

 

 

 

 

 

 

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.