我有这个输入框,它接受输入,文本被附加在一个<div>称为“父”的内部,作为另一个<div>称为“子”。
编码:
const send = () => {
const parent = document.querySelector('.parent');
const text = document.querySelector('.text').value;
const newtext = document.createElement("div");
newtext.classList.add(".child");
newtext.innerHTML = "Me: " + text;
parent.appendChild(newtext);
}
* {
margin: 0;
padding: 0;
}
body {
position: absolute;
}
.parent {
position: relative;
width: 100%;
}
.child {
position: relative;
height: fit-content;
width: fit-content;
text-align: right;
max-width: 50%;
white-space: pre-line;
word-wrap: break-word;
}
<html>
<body>
<input type="text" class="text">
<button onclick="send()">Send</button>
<div class="parent"></div>
</body>
</html>
我想让<div>“孩子”类出现在右侧。但是标签的未知宽度使得它很难与leftCSS 中的属性对齐。每次发布新文本时如何将其显示在右侧?