The jquery method to determine whether an element contains an attribute: first use the div tag to create text and set its id to test; then obtain the div object through the element name and use the [attr()] method to obtain the id attribute; Finally, use the if statement to determine whether the obtained id value is undefined.
The operating environment of this tutorial: Windows 7 system, JavaScript version 1.8.5. This method is suitable for all brands of computers.
Related free learning recommendations: JavaScript (video)
jquery method to determine whether an element contains an attribute:
1. Create a new html file, named test.html, to explain jquery's judgment of whether an element has a certain attribute.
2. In the test.html file, introduce the jquery.min.js library file and successfully load the file before you can use the methods in jquery.
3. In the test.html file, use the div tag to create a line of text and set its id to test. It is mainly used to determine whether the div has this attribute.
4. In the test.html file, use the button tag to create a button with the button name "Judge id attribute".
5. In the test.html file, bind the onclick event to the button button. When the button is clicked, the pan() function is executed.
6. In the pan()
function, obtain the div object through the div element name and use the attr()
method to obtain it id attribute, and then use the if statement to determine whether the obtained id value is undefined (that is, there is no id). If the id attribute exists, it will prompt to output the id attribute. Otherwise, it will prompt "there is no id attribute".
7. Open the test.html file in the browser and click the button to see the effect.
For more programming-related knowledge, please visit: Programming Learning! !
The above is the detailed content of How to determine whether an element contains an attribute in jquery. For more information, please follow other related articles on the PHP Chinese website!