Hello everybody, so iam Trying to insert an HTML Element into an another HTML Element using Javascript.
我尝试使用 .insertAdjacentHTML
但它会将其插入到目标元素附近,因为这些位置不适合我...并且 .html
将其作为文本插入格式不是 HTML 格式
这是我尝试编码的内容:-
SendClientMessage(COLORS_.white, "15px", "none", '<div class="ls-files">' + CMDS_List + '</div>');
function SendClientMessage(color, font, align = 'none', message)
{
if(align !== "none")
{
output_.insertAdjacentHTML('beforeend', '<p style="color: ' + color + '; font-size: ' + font + '; text-align: ' + align + ';">' + message + '</p>');
}
else if(align === "none")
{
output_.insertAdjacentHTML('beforeend', '<p style="color: ' + color + '; font-size: ' + font + ';">' + message + '</p>');
}
}
输出:-
<p style="color: #FFFFFF; font-size: 15px;"></p>
<div class="ls-files">clear,clock,dati,ping,uname,whoami,cmd,</div>
最佳答案
您只能使用 insertAdjacentHTML 将 span 或 a 等内联元素放入 p 标记中,因为它使用 html 解析器来检查在插入内容之前正确格式化 html。
let CMDS_List = 'clear,clock,dati,ping,uname,whoami,cmd,';
SendClientMessage('white', "15px", "none", '<span class="ls-files">' + CMDS_List + '</span>');
function SendClientMessage(color, font, align = 'none', message) {
let output_ = document.getElementById('output');
if (align !== "none") {
output_.insertAdjacentHTML('beforeend', '<p style="color: ' + color + '; font-size: ' + font + '; text-align: ' + align + ';">' + message + '</p>');
} else if (align === "none") {
output_.insertAdjacentHTML('beforeend', '<p style="color: ' + color + '; font-size: ' + font + ';">' + message + '</p>');
}
}
body {
background: #999;
<div id="output"></div>
关于javascript - 尝试使用 Javascript 将 HTML 元素插入另一个 HTML 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47848607/