0
可能性の重複取得:私は2つの異なる状況でdivタグ内のテキストを表示したい
Can jQuery provide the tag name?要素のタグ名
を:
- I最初の子要素の上にカーソルを置いてください。私はテキストをidとタグ要素の名前として見たいと思っています。
- 他の要素(最初の子ではない)の上にカーソルを置くと、「これは最初の子ではありません」というテキストが表示されます。
HTML:
<ul id="ul1">ul1
<li id="aa">1</li>
<li id="bb">2</li>
<li id="cc"class="elm">3</li>
<li id="dd">4</li>
<li id="ee">5</li>
</ul>
<ul id="ul2">ul2
<li id="ul2-1">1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
</ul><br/>
<div>element-name</div>
CSS:
div,li{ width:40px; height:40px; margin:10px;
float:left; border:2px blue solid;
padding:2px; }
ul{ width:100px; height:10px; margin:10px;
float:left; border:2px blue solid;
padding:2px; }
span { font-size:14px; }
p { clear:left; margin:10px }
のjQuery:あなたがタグを取得することができます
var $curr = $(".elm");
$curr.css("background", "#f99");
$("*:first-child")
.css("background", "#cac")
.hover(function() {
$(this).css("background","green");
$('div').text(this.id+$(this).tagname());
}, function() {
$(this).css("background", "#cac");
});