我有这个html:
<span class="msg-container">
<span class="msg"></span>
A message here
</span>
我想使用jQuery来查找所有的msg-container元素,获取"A message here“文本,设置title属性,并删除"A message here”文本节点。
所以,在执行之后,我的DOM应该是这样的:
<span class="msg-container" title="A message here">
<span class="msg"></span>
</span>
我该如何实现这一点?
发布于 2012-04-20 06:36:26
$('.msg-container').each(function(){
var that = $(this);
that.attr('title', that.text());
var children = that.find('.msg').clone();
that.html('').append(children);
});
发布于 2012-04-20 06:32:14
我认为你需要使用for-each函数
$(".msg-container").each(function(){
var child = $(this).children(".msg").html();
var text=$(this).html("");
$(this)attr("title" , text)
$(this).append(child);
});
发布于 2012-04-20 06:34:57
试一试
$(function() {
$(".msg-container").each(function() {
var txt = $(this).text();
var children = $(this).children();
$(this).attr("title",txt.trim())
.text("").append(children);
});
});
https://stackoverflow.com/questions/10237729
复制相似问题