【发布时间】:2012-08-08 18:53:31
【问题描述】:
我在这个链接上捎带... Extract the Text in a Element with JQuery
我喜欢提取元素中的文本,然后将其附加到这样的属性中......
jQuery
$(document).ready(function() {
var text;
$(".parent span").contents().each(function(i) {
if(this.nodeName == "#text") text = $(this).text();
$(".child").attr("ref", text);
});
});
HTML
<div class="parent">
<div class="child"> </div>
<span><strong>bla bla bla</strong>Child-1</span> </div>
<div class="parent">
<div class="child"> </div>
<span><strong>bla bla bla</strong>Child-2</span>
</div>
我不断在每个父母的参考中得到“Child-1”。
【问题讨论】:
-
$('#bla2').attr({ref:$('#bla').text()}); -
您要提取什么文本并将其附加到哪里?这样做的原因是什么?
标签: jquery text attributes clone extraction