如何获得div的第一个span元素并更改类

use*_*100 12 jquery

我有一个跨度的div

<div id="div1">
    <span class="">hello</span>
</div>
Run Code Online (Sandbox Code Playgroud)

当我点击div我想改变div的第一个span元素的类

$('#div1').click(function() {
    // ... check if first span element inside the div .. if there and change the class..    
});  
Run Code Online (Sandbox Code Playgroud)

And*_*ong 19

$('#div1').click(function() {    
   $('span:first', this).prop('class', 'newClassName');
})
Run Code Online (Sandbox Code Playgroud)

http://api.jquery.com/first-selector/

http://jsfiddle.net/BUBb9/1/


Hem*_*lia 6

使用

 $("div span:first-child")
Run Code Online (Sandbox Code Playgroud)

如下:

$('#div1').click(function() {    
   $('span:first', this).prop('class', 'ClassName');
})
Run Code Online (Sandbox Code Playgroud)

参考http://api.jquery.com/first-child-selector/