使用 jQuery 选择内部文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1644342/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Select inner text with jQuery
提问by Sven Klouem
<div class="boxen checkedzahlen" id="box41_0_1">
41
<input type="checkbox" value="1" name="cb41_0_1" id="cb41_0_1" checked="checked"/>
</div>
Something like this is given, how can I animate the text 41
, if $(this)
(the class boxen
) is clicked?
给出了这样的东西41
,如果$(this)
(类boxen
)被点击,我如何为文本设置动画?
this > *
doesn't work. Neither does this:children
.
this > *
不起作用。也不行this:children
。
回答by Mike Chamberlain
Because of the slightly confusing title, some people (like me) may be coming to this question looking for how to get the textual contentof a DOM element. You can accomplish this by:
由于标题有点混乱,有些人(比如我)可能会来这个问题寻找如何获取DOM 元素的文本内容。您可以通过以下方式完成此操作:
$("#myselector").text() // returns the content of the element as plain text
回答by tuanvt
$("#divID").html() would get the text inside.
$("#divID").html() 会得到里面的文本。
回答by Topher Fangio
In order to select the div, you would use the following:
为了选择 div,您将使用以下内容:
$('#box41_0_1')
In order to animate the whole box, you could do
为了动画整个框,你可以做
$('#box41_0_1').fadeOut().fadeIn();
or some other animation effect.
或其他一些动画效果。
Edit:If you want to select only the inner text, you could try wrappingthe inner text with a div, but with the code you provided, that would also select the checkbox I believe. Code example:
编辑:如果您只想选择内部文本,您可以尝试用 div包装内部文本,但使用您提供的代码,这也会选中我相信的复选框。代码示例:
$('#box41_0_1').wrapInner("<span id='span41_0_1'></span>");
$('#span41_0_1').fadeOut().fadeIn();
回答by Karl Swedberg
You could wrap the contents of the div in a span and then move the input outside of that span. Then you can animate the span. Something like this:
您可以将 div 的内容包装在一个跨度中,然后将输入移动到该跨度之外。然后您可以为跨度设置动画。像这样的东西:
$('.boxen').each(function() {
var $thisBox = $(this);
$thisBox.wrapInner('<span></span>');
$thisBox.find('input').appendTo($thisBox);
});
$('.boxen span').animate({fontSize: '28px'}, 400);
You could also mix and match straight DOM scripting with jQuery, like so:
您还可以将直接 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);
Either way should work.
无论哪种方式都应该有效。