怎么使用jquery获得标签元素名
2个回答
展开全部
怎么获得标签元素名,你通过使用tagName函数来实现。在此有两种方法来使用她:
1).get(0).tagName
通过使含脊颤用它的一个类名来选择一个元素,然后通过使用.get(0).tagName 函数来显示其标签名。
$('.classTag1').get(0).tagName;
2).[0].tagName
2. 使用它的一个类名来选择一个元素,然后使用.[0].tagName函数来显示标签名。
$('.classTag1')[0].tagName;
实例:
<html>
<head>
<title>jQuery Get Tag Name</title>
<scripttype="text/javascript"src="jquery-1.3.2.min.js"></script>
</head>
<scripttype="text/javascript">
$(document).ready(function(){
var $tag = $('p')[0].tagName; //'P'谈败
alert($tag);
var $tag = $('.classTag1')[0].tagName; //'DIV'
alert($tag);
var $tag = $('#idTag1')[0].tagName; //'DIV'
alert($tag);
var $tag = $('p').get(0).tagName; //'P'
alert($tag);
var $tag = $('.classTag1').get(0).tagName; //'DIV'
alert($tag);
var $tag = $('#idTag1').get(0).tagName; //'DIV'
alert($tag);
});
</script>
<body>
<h1>jQuery Get Tag Name</h1>
<p>
This is paragrah 1
</p>
<divclass="classTag1">
This is class='classTag1'
</div>
<divid="idTag1">
This is id='idTag1'
</div>
<野郑/body>
</html>
1).get(0).tagName
通过使含脊颤用它的一个类名来选择一个元素,然后通过使用.get(0).tagName 函数来显示其标签名。
$('.classTag1').get(0).tagName;
2).[0].tagName
2. 使用它的一个类名来选择一个元素,然后使用.[0].tagName函数来显示标签名。
$('.classTag1')[0].tagName;
实例:
<html>
<head>
<title>jQuery Get Tag Name</title>
<scripttype="text/javascript"src="jquery-1.3.2.min.js"></script>
</head>
<scripttype="text/javascript">
$(document).ready(function(){
var $tag = $('p')[0].tagName; //'P'谈败
alert($tag);
var $tag = $('.classTag1')[0].tagName; //'DIV'
alert($tag);
var $tag = $('#idTag1')[0].tagName; //'DIV'
alert($tag);
var $tag = $('p').get(0).tagName; //'P'
alert($tag);
var $tag = $('.classTag1').get(0).tagName; //'DIV'
alert($tag);
var $tag = $('#idTag1').get(0).tagName; //'DIV'
alert($tag);
});
</script>
<body>
<h1>jQuery Get Tag Name</h1>
<p>
This is paragrah 1
</p>
<divclass="classTag1">
This is class='classTag1'
</div>
<divid="idTag1">
This is id='idTag1'
</div>
<野郑/body>
</html>
2017-11-06 · 百度知道合伙人官方认证企业
育知同创教育
1【专注:Python+人工智能|Java大数据|HTML5培训】 2【免费提供名师直播课堂、公开课及视频教程】 3【地址:北京市昌平区三旗百汇物美大卖场2层,微信公众号:yuzhitc】
向TA提问
关注
展开全部
<pre style="background:#F7F7F9"><span style="color:#009900"><</span><strong>html</strong><span style="color:#009900">></span></pre>
<pre style="background:#F7F7F9"><span style="color:#009900"><</span><strong>head</strong><span style="color:#009900">></span></pre>
<pre style="background:#F7F7F9"><span style="color:#009900"><</span><strong>title</strong><span style="color:#009900">></span><span style="color:#333333">jQuery Get Tag Name</span><span style="color:#009900"><</span><span style="color:#66CC66">/</span><strong>title</strong><span style="color:#009900">></span></pre>
<pre style="background:#F7F7F9"><span style="color:#333333"> </span></pre>
<pre style="background:#F7F7F9"><span style="color:#009900"><</span><strong>script</strong><span style="color:#000066">type</span><span style="color:#66CC66">物大=</span><span style="color:red">"text/javascript"</span><span style="color:#000066">src</span><span style="color:#66CC66">=</span><span style="color:red">"jquery-1.3.2.min.js"</span><span style="color:#009900">><</span><span style="color:#66CC66">/</span><strong>script</strong><span style="color:#009900">></span></pre>
<pre style="background:#F7F7F9"><span style="color:#333333"> </span></pre>
<pre style="background:#F7F7F9"><span style="color:#009900"><</span><span style="罩橘竖color:#66CC66">/</span><strong>head</strong><span style="color:#009900">></span></pre>
<pre style="background:#F7F7F9"><span style="color:#333333"> </span></pre>
<pre style="background:#F7F7F9"><span style="color:#009900"><</span><strong>script</strong><span style="color:#000066">type</span><span style="color:#66CC66">=</span><span style="color:red">"text/javascript"</span><span style="color:#009900">></span></pre>
<pre style="background:#F7F7F9"><span style="color:#333333"> </span></pre>
<pre style="background:#F7F7F9"><span style="color:#333333">$(document).ready(function(){</span></pre>
<pre style="background:#F7F7F9"><span style="color:#333333"> </span></pre>
<pre style="background:#F7F7F9"><span style="color:#333333"> var $tag = $('p')[0].tagName; //'P'</span></pre>
<pre style="background:#F7F7F9"><span style="color:#333333"> </span><span style="color:#333333">alert($tag);</span></伍粗pre>
<pre style="background:#F7F7F9"><span style="color:#333333"> </span></pre>
<pre style="background:#F7F7F9"><span style="color:#333333"> var $tag = $('.classTag1')[0].tagName; //'DIV'</span></pre>
<pre style="background:#F7F7F9"><span style="color:#333333"> alert($tag);</span></pre>
<pre style="background:#F7F7F9"><span style="color:#333333"> </span></pre>
<pre style="background:#F7F7F9"><span style="color:#333333"> var $tag = $('#idTag1')[0].tagName; //'DIV'</span></pre>
<pre style="background:#F7F7F9"><span style="color:#333333"> alert($tag);</span></pre>
<pre style="background:#F7F7F9"><span style="color:#333333"> </span></pre>
<pre style="background:#F7F7F9"><span style="color:#333333"> var $tag = $('p').get(0).tagName; //'P'</span></pre>
<pre style="background:#F7F7F9"><span style="color:#333333"> alert($tag);</span></pre>
<pre style="background:#F7F7F9"><span style="color:#333333"> </span></pre>
<pre style="background:#F7F7F9"><span style="color:#333333"> var $tag = $('.classTag1').get(0).tagName; //'DIV'</span></pre>
<pre style="background:#F7F7F9"><span style="color:#333333"> alert($tag);</span></pre>
<pre style="background:#F7F7F9"><span style="color:#333333"> </span></pre>
<pre style="background:#F7F7F9"><span style="color:#333333"> var $tag = $('#idTag1').get(0).tagName; //'DIV'</span></pre>
<pre style="background:#F7F7F9"><span style="color:#333333"> </span><span style="color:#333333">alert($tag); </span></pre>
<pre style="background:#F7F7F9"><span style="color:#333333"> </span></pre>
<pre style="background:#F7F7F9"><span style="color:#333333">});</span></pre>
<pre style="background:#F7F7F9"><span style="color:#333333"> </span></pre>
<pre style="background:#F7F7F9"><span style="color:#009900"><</span><span style="color:#66CC66">/</span><strong>script</strong><span style="color:#009900">></span></pre>
<pre style="background:#F7F7F9"><span style="color:#009900"><</span><strong>body</strong><span style="color:#009900">></span></pre>
<pre style="background:#F7F7F9"><span style="color:#333333"> </span></pre>
<pre style="background:#F7F7F9"><span style="color:#009900"><</span><strong>h1</strong><span style="color:#009900">></span><span style="color:#333333">jQuery Get Tag Name</span><span style="color:#009900"><</span><span style="color:#66CC66">/</span><strong>h1</strong><span style="color:#009900">></span></pre>
<pre style="background:#F7F7F9"><span style="color:#333333"> </span></pre>
<pre style="background:#F7F7F9"><span style="color:#333333"> </span><span style="color:#009900"><</span><strong>p</strong><span style="color:#009900">></span></pre>
<pre style="background:#F7F7F9"><span style="color:#333333"> This is paragrah 1</span></pre>
<pre style="background:#F7F7F9"><span style="color:#333333"> </span><span style="color:#009900"><</span><span style="color:#66CC66">/</span><strong>p</strong><span style="color:#009900">></span></pre>
<pre style="background:#F7F7F9"><span style="color:#333333"> </span></pre>
<pre style="background:#F7F7F9"><span style="color:#333333"> </span><span style="color:#009900"><</span><strong>div</strong><span style="color:#000066">class</span><span style="color:#66CC66">=</span><span style="color:red">"classTag1"</span><span style="color:#009900">></span></pre>
<pre style="background:#F7F7F9"><span style="color:#333333"> This is class='classTag1'</span></pre>
<pre style="background:#F7F7F9"><span style="color:#333333"> </span><span style="color:#009900"><</span><span style="color:#66CC66">/</span><strong>div</strong><span style="color:#009900">></span></pre>
<pre style="background:#F7F7F9"><span style="color:#333333"> </span></pre>
<pre style="background:#F7F7F9"><span style="color:#333333"> </span><span style="color:#009900"><</span><strong>div</strong><span style="color:#000066">id</span><span style="color:#66CC66">=</span><span style="color:red">"idTag1"</span><span style="color:#009900">></span></pre>
<pre style="background:#F7F7F9"><span style="color:#333333"> This is id='idTag1'</span></pre>
<pre style="background:#F7F7F9"><span style="color:#333333"> </span><span style="color:#009900"><</span><span style="color:#66CC66">/</span><strong>div</strong><span style="color:#009900">></span></pre>
<pre style="background:#F7F7F9"><span style="color:#333333"> </span></pre>
<pre style="background:#F7F7F9"><span style="color:#009900"><</span><span style="color:#66CC66">/</span><strong>body</strong><span style="color:#009900">></span></pre>
<pre style="background:#F7F7F9"><span style="color:#009900"><</span><span style="color:#66CC66">/</span><strong>html</strong><span style="color:#009900">></span></pre>
<pre style="background:#F7F7F9"><span style="color:#009900"><</span><strong>head</strong><span style="color:#009900">></span></pre>
<pre style="background:#F7F7F9"><span style="color:#009900"><</span><strong>title</strong><span style="color:#009900">></span><span style="color:#333333">jQuery Get Tag Name</span><span style="color:#009900"><</span><span style="color:#66CC66">/</span><strong>title</strong><span style="color:#009900">></span></pre>
<pre style="background:#F7F7F9"><span style="color:#333333"> </span></pre>
<pre style="background:#F7F7F9"><span style="color:#009900"><</span><strong>script</strong><span style="color:#000066">type</span><span style="color:#66CC66">物大=</span><span style="color:red">"text/javascript"</span><span style="color:#000066">src</span><span style="color:#66CC66">=</span><span style="color:red">"jquery-1.3.2.min.js"</span><span style="color:#009900">><</span><span style="color:#66CC66">/</span><strong>script</strong><span style="color:#009900">></span></pre>
<pre style="background:#F7F7F9"><span style="color:#333333"> </span></pre>
<pre style="background:#F7F7F9"><span style="color:#009900"><</span><span style="罩橘竖color:#66CC66">/</span><strong>head</strong><span style="color:#009900">></span></pre>
<pre style="background:#F7F7F9"><span style="color:#333333"> </span></pre>
<pre style="background:#F7F7F9"><span style="color:#009900"><</span><strong>script</strong><span style="color:#000066">type</span><span style="color:#66CC66">=</span><span style="color:red">"text/javascript"</span><span style="color:#009900">></span></pre>
<pre style="background:#F7F7F9"><span style="color:#333333"> </span></pre>
<pre style="background:#F7F7F9"><span style="color:#333333">$(document).ready(function(){</span></pre>
<pre style="background:#F7F7F9"><span style="color:#333333"> </span></pre>
<pre style="background:#F7F7F9"><span style="color:#333333"> var $tag = $('p')[0].tagName; //'P'</span></pre>
<pre style="background:#F7F7F9"><span style="color:#333333"> </span><span style="color:#333333">alert($tag);</span></伍粗pre>
<pre style="background:#F7F7F9"><span style="color:#333333"> </span></pre>
<pre style="background:#F7F7F9"><span style="color:#333333"> var $tag = $('.classTag1')[0].tagName; //'DIV'</span></pre>
<pre style="background:#F7F7F9"><span style="color:#333333"> alert($tag);</span></pre>
<pre style="background:#F7F7F9"><span style="color:#333333"> </span></pre>
<pre style="background:#F7F7F9"><span style="color:#333333"> var $tag = $('#idTag1')[0].tagName; //'DIV'</span></pre>
<pre style="background:#F7F7F9"><span style="color:#333333"> alert($tag);</span></pre>
<pre style="background:#F7F7F9"><span style="color:#333333"> </span></pre>
<pre style="background:#F7F7F9"><span style="color:#333333"> var $tag = $('p').get(0).tagName; //'P'</span></pre>
<pre style="background:#F7F7F9"><span style="color:#333333"> alert($tag);</span></pre>
<pre style="background:#F7F7F9"><span style="color:#333333"> </span></pre>
<pre style="background:#F7F7F9"><span style="color:#333333"> var $tag = $('.classTag1').get(0).tagName; //'DIV'</span></pre>
<pre style="background:#F7F7F9"><span style="color:#333333"> alert($tag);</span></pre>
<pre style="background:#F7F7F9"><span style="color:#333333"> </span></pre>
<pre style="background:#F7F7F9"><span style="color:#333333"> var $tag = $('#idTag1').get(0).tagName; //'DIV'</span></pre>
<pre style="background:#F7F7F9"><span style="color:#333333"> </span><span style="color:#333333">alert($tag); </span></pre>
<pre style="background:#F7F7F9"><span style="color:#333333"> </span></pre>
<pre style="background:#F7F7F9"><span style="color:#333333">});</span></pre>
<pre style="background:#F7F7F9"><span style="color:#333333"> </span></pre>
<pre style="background:#F7F7F9"><span style="color:#009900"><</span><span style="color:#66CC66">/</span><strong>script</strong><span style="color:#009900">></span></pre>
<pre style="background:#F7F7F9"><span style="color:#009900"><</span><strong>body</strong><span style="color:#009900">></span></pre>
<pre style="background:#F7F7F9"><span style="color:#333333"> </span></pre>
<pre style="background:#F7F7F9"><span style="color:#009900"><</span><strong>h1</strong><span style="color:#009900">></span><span style="color:#333333">jQuery Get Tag Name</span><span style="color:#009900"><</span><span style="color:#66CC66">/</span><strong>h1</strong><span style="color:#009900">></span></pre>
<pre style="background:#F7F7F9"><span style="color:#333333"> </span></pre>
<pre style="background:#F7F7F9"><span style="color:#333333"> </span><span style="color:#009900"><</span><strong>p</strong><span style="color:#009900">></span></pre>
<pre style="background:#F7F7F9"><span style="color:#333333"> This is paragrah 1</span></pre>
<pre style="background:#F7F7F9"><span style="color:#333333"> </span><span style="color:#009900"><</span><span style="color:#66CC66">/</span><strong>p</strong><span style="color:#009900">></span></pre>
<pre style="background:#F7F7F9"><span style="color:#333333"> </span></pre>
<pre style="background:#F7F7F9"><span style="color:#333333"> </span><span style="color:#009900"><</span><strong>div</strong><span style="color:#000066">class</span><span style="color:#66CC66">=</span><span style="color:red">"classTag1"</span><span style="color:#009900">></span></pre>
<pre style="background:#F7F7F9"><span style="color:#333333"> This is class='classTag1'</span></pre>
<pre style="background:#F7F7F9"><span style="color:#333333"> </span><span style="color:#009900"><</span><span style="color:#66CC66">/</span><strong>div</strong><span style="color:#009900">></span></pre>
<pre style="background:#F7F7F9"><span style="color:#333333"> </span></pre>
<pre style="background:#F7F7F9"><span style="color:#333333"> </span><span style="color:#009900"><</span><strong>div</strong><span style="color:#000066">id</span><span style="color:#66CC66">=</span><span style="color:red">"idTag1"</span><span style="color:#009900">></span></pre>
<pre style="background:#F7F7F9"><span style="color:#333333"> This is id='idTag1'</span></pre>
<pre style="background:#F7F7F9"><span style="color:#333333"> </span><span style="color:#009900"><</span><span style="color:#66CC66">/</span><strong>div</strong><span style="color:#009900">></span></pre>
<pre style="background:#F7F7F9"><span style="color:#333333"> </span></pre>
<pre style="background:#F7F7F9"><span style="color:#009900"><</span><span style="color:#66CC66">/</span><strong>body</strong><span style="color:#009900">></span></pre>
<pre style="background:#F7F7F9"><span style="color:#009900"><</span><span style="color:#66CC66">/</span><strong>html</strong><span style="color:#009900">></span></pre>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询