Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 将文本附加到div,而不是html_Jquery - Fatal编程技术网

Jquery 将文本附加到div,而不是html

Jquery 将文本附加到div,而不是html,jquery,Jquery,我有这个,它是有效的: local.Source = $('article.hero-unit').html(); $('#Source').text(local.Source); 但我想做的是: local.Source = $('article.hero-unit').html(); $('fieldset').append(local.Source); 问题是append方法附加html,并且没有转义

我有这个,它是有效的:

local.Source = $('article.hero-unit').html();
$('#Source').text(local.Source);
但我想做的是:

local.Source = $('article.hero-unit').html();
$('fieldset').append(local.Source);
问题是append方法附加html,并且没有转义Try

local.Source = $('article.hero-unit').html();
var $fieldset = $('fieldset');
$fieldset.text($fieldset.text()+local.Source);

不带参数调用
.text()
,将获取元素中已有的文本。所有这一切只需添加
local.Source
的html即可

我会将该文本放入
元素中:

$('<span>', {
    text: local.Source
}).appendTo('fieldset');
$(“”{
文本:local.Source
}).appendTo(“字段集”);

我认为这样做很有效:

local.Source = $('article.hero-unit').html();
local.Legend = $('fieldset legend').text();
$('fieldset').text(local.Source);
$('fieldset').prepend('<legend>' + local.Legend + '</legend>');
local.Source=$('article.hero unit').html();
local.Legend=$('fieldset Legend').text();
$('fieldset').text(local.Source);
$('fieldset').prepend(“”+local.Legend+“”);

您好,我想知道您为什么认为这是一个比我建议的更好的解决方案?(顺便说一句,你真是个怪兽:)干得好!)@本贾明鲁恩鲍姆:这只是另一种方式。包装器元素使您可以设置文本块的样式,并便于以后删除它们。好吧,我的问题是我的代码丢失了。@Phillip我不确定我是否理解您的问题,我假设您的图例标记嵌套在您的字段集中,这意味着我的代码应该可以工作。您是否介意创建一个提琴(codepen.io或jsfiddle.net或jsbin.com)来说明此代码的问题,或者至少说明预期的输入和输出是什么?@Phillip您最终做了什么?出了什么问题?