以下範例示範如何透過 jQuery 物件的 get(0) 取得原生 DOM 元素,再用 tagName 讀取其標籤名稱,並示範 toLowerCase() 與 toUpperCase() 的大小寫轉換。
<script type="text/javascript" src="jquery1.4.js"></script>
<script type="text/javascript">
$(function(){
$(".test li,.test dd").click(function(){
alert('本身為大寫:' + $(this).get(0).tagName +
'\n利用 toLowerCase() 轉換為小寫:' + $(this).get(0).tagName.toLowerCase() +
'\n利用 toUpperCase() 轉換為大寫:' + $(this).get(0).tagName.toUpperCase());
})
});
</script>
<div class="test">
<ul>
<li>lili</li>
<li>lili</li>
</ul>
<dl>
<dd>dddd</dd>
<dd>dddd</dd>
</dl>
</div>
點擊任一 li 或 dd 時,便會跳出該元素的標籤名稱。可以注意到 tagName 回傳的是大寫,若需要小寫比對,套上 toLowerCase() 即可。