0

我有 div 列表,我想要实现的是删除 child<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 唯一的唯一字段是 data-square 。访问http://jsfiddle.net/jm4eb/11/所以当第一个div的内部div而不是第二个div的内部div点击时。第二个div的内部div在替换之前被删除

4

2 回答 2

2

如何更改文本值而不是删除然后创建一个新值?

$( '.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 没有 shim 的方法firstElementChild

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

于 2012-04-22T17:25:50.763 回答
0

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

我错过了你的问题吗?

于 2012-04-22T17:56:16.917 回答