Home  >  Article  >  Web Front-end  >  How to get element tags in jquery

How to get element tags in jquery

王林
王林Original
2020-11-19 16:52:574200browse

Jquery method of getting element tags: You can get element tags by using the tagName attribute, such as [var name = $("#p").get(0).tagName; alert(name);].

How to get element tags in jquery

html code is as follows:

(Learning video sharing: jquery video tutorial)

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="../js/jquery-3.2.1.js"></script>
</head>
<body>
    <p id ="p" >这是p标签</p>
<script>
  
</script>
</body>

</html>

The method is as follows:

Method 1: Use the tagName attribute to implement

var  name = $("#p").get(0).tagName;
alert(name);

var  name = $("#p")[0].tagName;
alert(name);

The output results are: p

Method 2:

var  name = $("#p").prop("tagName");
alert(name);

The result: p

Method three:

My own encapsulated method

var  name = $("#p").prop("nodeName");
alert(name);

Result: p

Related recommendations: js tutorial

The above is the detailed content of How to get element tags in jquery. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn