删除子分区

remove child div

本文关键字:分区 删除      更新时间:2023-09-26

我有div列表,我想要实现的是删除子<div class="lol">999</div>,并将其替换为<div class="lol">111</div>

我确实知道如何获取它们两者的数据平方。我尝试使用$(this).removeClass('column div');但它效果不佳。

访问 http://jsfiddle.net/jm4eb/5/以前:

<div class="column" data-square="1-4">
<div class="green">999</div>
</div>

<div class="column" data-square="1-5">
<div class="blue">111</div>
</div>

<div class="column" data-square="1-4">
</div>

<div class="column" data-square="1-5">
<div class="green">999</div>
</div>

嗨,这就是应用程序的工作方式。 假设有 10 个div 框,每个框都有一个内框 12 ,用户单击一个div 并单击另一个。 因此,应删除单击的第二个div 的内部div,并替换为第一次单击div 的div 内部。 但是外部div 他们拥有的唯一唯一字段是 数据平方 .访问 http://jsfiddle.net/jm4eb/11/所以当第一个div的内部div而不是第二个div的内部div单击时,第二个div的内部div在替换之前被删除

更改文本值而不是删除然后创建一个新值怎么样?

$( '.column' ).each( function () {
    // Save $( this ) and $( this ).next() since we're going to use them multiple times
    var self = $( this ),
        next = self.next()
    // For each of them, save the firstChild's text
    var val = self.children().first().text()
    // Now, check if the next DIV exists
    if ( next ) {
        // If it does, change the text of its first child with the "val" variable
        next.children().first().text( val )
    }
} )

编辑:将.find( '>:first-child' )替换为.children().first()。不敢相信jQuery没有一种方法来填充firstElementChild

@Esailija也觉得缺少这种方法很奇怪。所以他创建了一个jQuery插件。这意味着我可以使用self.firstChild().text()而不是self.children().first().text()

如果我理解正确,您希望将div 用户的内容首先单击 intio 他们单击的第二个div。由于您已经选择了有问题的 dv(通过他们的单击)并且您拥有它的数据平方,因此只需复制 html 即可。例如:http://jsfiddle.net/jm4eb/6/如果要清空第一个div 的内容,只需在第一次单击时添加.html(""),或者保存对单击的div 的引用,并在他们第二次单击时对保存的引用运行.html("")

我错过了你的问题吗?