使用jQuery选择内部文本
41
给出了这样的东西,如果单击$(this)
(类boxen
),我该如何为文本41
设置动画?
this > *
不起作用。 这也不是this:children
。
由于标题略显混乱,有些人(像我一样)可能会想到如何获取DOM元素的文本内容 。 你可以通过以下方式完成
$("#myselector").text() // returns the content of the element as plain text
$(“#divID”)。html()会得到里面的文字。
要选择div,您将使用以下内容:
$('#box41_0_1')
为了给整个盒子制作动画,你可以做到
$('#box41_0_1').fadeOut().fadeIn();
或其他一些动画效果 。
编辑:如果你只想选择内部文本,你可以尝试用div 包装内部文本,但是使用你提供的代码,也可以选择我相信的复选框。 代码示例:
$('#box41_0_1').wrapInner(""); $('#span41_0_1').fadeOut().fadeIn();
您可以在一个范围中包装div的内容,然后将输入移动到该范围之外。 然后你可以设置跨度的动画。 像这样的东西:
$('.boxen').each(function() { var $thisBox = $(this); $thisBox.wrapInner(''); $thisBox.find('input').appendTo($thisBox); }); $('.boxen span').animate({fontSize: '28px'}, 400);
您还可以将直接DOM脚本与jQuery混合搭配,如下所示:
$('.boxen').each(function() { var newSpan = document.createElement('span'); newSpan.innerHTML = this.firstChild.nodeValue; this.firstChild.nodeValue = ''; $(this).prepend(newSpan); }); $('.boxen span').animate({fontSize: '28px'}, 400);
无论哪种方式都应该有效。