简体   繁体   English

jQuery在textarea中显示html排除元素

[英]jQuery display html in textarea exclude an element

I have 4 divs in a container. 我在一个容器中有4个div。 I want to display the html of the container which contains the divs in the textarea. 我想在textarea中显示包含div的容器的html。 I'm able to do this. 我能够做到这一点。 The issue is, i don't want to get all the html of the container. 问题是,我不想获取容器的所有html。 I don't want to get #iv #three. 我不想得到#iv#3。 I want to copy all the html of the container except div #three. 我想复制除div #three之外的所有html容器。 I could use $('#three').remove() but i don't want to remove the div, I just don't want to copy it's html value to textarea. 我可以使用$('#three').remove()但我不想删除div,我只是不想将其html值复制到textarea。 Check jsfiddle http://jsfiddle.net/rzfPP/ 检查jsfiddle http://jsfiddle.net/rzfPP/

<div id="container">
  <div id="one">test 1 </div>
  <div id="two">test 2 </div>
  <div id="three">test 3 </div>
  <div id="four">test 4 </div>
</div>    
<textarea id="save"></textarea>

var x = $('#container').html();
$('#save').val(x);

Try this 尝试这个

$("#container").clone().find("#three").remove().end().html();

http://jsfiddle.net/rzfPP/21/ http://jsfiddle.net/rzfPP/21/

/*
var x = $('#container').html();
$('#save').val(x);
*/

var lol = $('#container').clone()
$(lol).find('#three').remove();


$('#save').val(lol.html());
$('#container').clone().find('#three').remove().end().html();

Technically this is illegal since you are duplicating IDs, but it works fine. 从技术上讲,这是非法的,因为您要复制ID,但是可以正常工作。

http://jsfiddle.net/rzfPP/33/ http://jsfiddle.net/rzfPP/33/

Edit: Someone beat me to it :( Oh well. 编辑:有人击败了我:(哦,不错。

var text = "";

$('#container div').each( function() {

   if ( this.id != "three" ) {

      text += $(this).html();

   }

});

$('#save').val( text );

http://jsfiddle.net/rzfPP/31/ http://jsfiddle.net/rzfPP/31/

Basically you check the divs inside #container one by one, and check their id. 基本上,您一一检查#container中的div,然后检查其ID。 If it's one you want, add their html to a string. 如果您要使用它,请将其html添加到字符串中。 Then at the end give that string as your textarea value. 然后最后将该字符串作为您的textarea值。

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

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