Javascript 添加和调整文本和符号
我正在使用以下代码向Javascript 添加和调整文本和符号,javascript,jquery,Javascript,Jquery,我正在使用以下代码向#mydiv添加文本: $('#mydiv').contents().filter(function() { return this.nodeType == 3 }).each(function(){ this.textContent = this.textContent.replace('', 'line 1 line 2'); }); 但是,我希望文本第2行位于下一行,使用换行符。我在文本第1行和第2行之间添加了\n,但这不起作用 此外,我想在文
#mydiv
添加文本:
$('#mydiv').contents().filter(function() {
return this.nodeType == 3
}).each(function(){
this.textContent = this.textContent.replace('', 'line 1 line 2');
});
但是,我希望文本第2行
位于下一行,使用换行符。我在文本第1行
和第2行
之间添加了\n
,但这不起作用
此外,我想在文本中使用一些符号(参见此处),但如果我将其添加到文本中,则无法识别:
$('#mydiv').contents().filter(function() {
return this.nodeType == 3
}).each(function(){
this.textContent = this.textContent.replace('', '✂ line 1 ✂ line 2');
});
在普通的CSS中,
和#9986
确实有效。问题在于您正在设置节点的文本内容。这意味着对任何HTML或字符实体进行编码。相反,您需要设置父节点的innerHTML
:
$('#mydiv').contents().filter(function () {
return this.nodeType == 3
}).each(function () {
this.parentNode.innerHTML = this.textContent.replace('', '✂ line 1 <br /> ✂ line 2');
});
$('#mydiv').contents().filter(函数(){
返回this.nodeType==3
}).每个(功能){
this.parentNode.innerHTML=this.textContent.replace(“”,✂第1行
✂第2行);
});
没问题,很乐意帮忙。在这个JSFIDLE中,我希望文本(“一段文本”)在它上面的文本(“这是长的”)下面直接包装,而不是在红色的X下面。你能帮忙吗?已经尝试了一段时间,但没有取得好结果:(