简体   繁体   English

jQuery .remove()之后清除div中的空间

[英]jquery clear the space in div after .remove()

Checkboxes are added to div1 dynamically: 复选框会动态添加到div1中:

$.each(json, function(idx, obj) {

$("#tbl1").append('<input type="checkbox" id='+obj.firstId+' onclick=nextPopulate('+obj.firstId+'); >'); }

On selecting these, checkboxes are added to div2 dynamically: 选择这些选项后,复选框会动态添加到div2中:

$.each(json, function(idx, obj) {

$("#tbl2").append('<label id="chk'+firstId+'"><input type="checkbox" id="'+firstId+'-'+secondId+'" ></label>'); }

On unchecking the checkbox in div1, the corresponding checkboxes(lable element) created in div2 are removed : 取消选中div1中的复选框时,将删除div2中创建的相应复选框(标签元素):

if(!($('#'+firstId).is(':checked')))

    $("[id^=chk"+firstId+"]").remove();

Lable content is removed.But, the space is still there.If i again select in div1, checkbox in div2 will be created only after this empty space. 标签内容被删除。但是,该空间仍然存在。如果我再次在div1中选择,则仅在此空白之后才会创建div2中的复选框。

How can i remove the space also while removing the content. 在删除内容的同时如何删除空间。 Will refreshing/reloading the div after removing will work ? 删除后刷新/重新加载div是否行得通?

If yes, then how is the syntax. 如果是,那么语法如何。 I don't want to hit the database/call the onclick fn again. 我不想访问数据库/再次调用onclick fn。 Just refresh the div to remove the space created while .remove() the label element. 只需刷新div即可删除.remove()标签元素时创建的空间。

The ".remove()" function is to remove children elements from the one which call, not to remove the one which call. “ .remove()”函数是从调用的那个元素中删除子元素,而不是从调用的那个元素中删除。 So with the code below, only checkbox input element is removed, while label element still remains. 因此,使用下面的代码,仅复选框输入元素被删除,而标签元素仍然保留。

$("[id^=chk"+firstId+"]").remove();

The element calling remove() should be "tbl2", and send the parameter "[id^=chk"+firstId+"]" into the function to define the child element about to remove as below: 调用remove()的元素应为“ tbl2”,并将参数“ [id ^ = chk” + firstId +“]”发送到函数中,以定义要删除的子元素,如下所示:

$("#tbl2").remove("[id^=chk"+firstId+"]");
.remove()

will work. 将工作。 But make sure that all the components especially 但是请确保所有组件,特别是

<br/>

etc are put inside the parent element and it is removed. 等放置在父元素内,并将其删除。 Then the space issue is resolved. 然后解决空间问题。 The next append will append to the correct space in the div. 下一个追加将追加到div中的正确空格。

The first answer will work. 第一个答案将起作用。 To remove <br> just do it: 要删除<br>只需执行以下操作:

$('br').remove(); // https://stackoverflow.com/questions/2513848/how-to-remove-nbsp-and-br-using-javascript-or-jquery

If you're have more id: 如果您有更多ID:

$("[id^=chk"+firstId+"]").remove('br');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM