1 回答
TA贡献1906条经验 获得超3个赞
这会将 id='1' 添加到小标签,并且每次添加图像时都会增加 1。只需为 id 声明一个全局,然后设置 para.id = id
var id=0;
function publish() {
var title = document.getElementById("title").value;
var description = document.getElementById("description").value;
var para = document.createElement("h3");
var node = document.createTextNode(title);
para.appendChild(node);
var element = document.getElementById("posts");
element.appendChild(para);
var para = document.createElement("small");
id++;
para.id=id;
var node = document.createTextNode("--".concat(description, "--"));
para.appendChild(node);
var image = document.getElementById("posts");
element.appendChild(para)
var image = document.createElement("img");
var imageInput = document.getElementById('image-file');
image.src = URL.createObjectURL(imageInput.files[0]);
image.style.height = '100px';
image.style.width = '100px';
para.appendChild(image);
}
<button id="publish-button" onclick="publish();">Publish</button>
<p>Title</p>
<input class="Title" id="title"></input>
<p>Description</p>
<input class="Description" id="description"></input>
<p>Images</p>
<input id="image-file" type="file" />
<ul id="posts">
</ul>
- 1 回答
- 0 关注
- 54 浏览
添加回答
举报