我有一个跨度的 div
<div id="div1">
<span class="">hello</span>
</div>
当我单击 div 时,我想更改 div 的唯一第一个 span 元素的类
$('#div1').click(function() {
// ... check if first span element inside the div .. if there and change the class..
});
我有一个跨度的 div
<div id="div1">
<span class="">hello</span>
</div>
当我单击 div 时,我想更改 div 的唯一第一个 span 元素的类
$('#div1').click(function() {
// ... check if first span element inside the div .. if there and change the class..
});
$('#div1').click(function() {
$('span:first', this).prop('class', 'newClassName');
})
利用
$("div span:first-child")
如下:
$('#div1').click(function() {
$('span:first', this).prop('class', 'ClassName');
})
$('#div1').click(function(){
var v = $(this).find('span').text();
});
这样就可以得到 span 的值。
您也可以使用 .html() 代替 .text() 方法。
多个答案有效。我的做法:
$('#div1').click(function() {
$(this).find('span').first().attr('class','newClassName');
});
$('#div1').click(function() {
$('#div1 span:first').attr('class', 'newClassName');
});