使用prepend()方法使用JavaScript模板文字
我有一个按钮元素的循环,这些元素用输出,而
循环通过PHP从MySQL数据库中调用的数据循环。
用户可以在此列表中添加一个按钮,我想添加新按钮,并且使用prepend()
方法在父元素上关联了HTML,因此它显示在列表的顶部。
我知道如何使用CreateElement并添加类名称和属性名称在各个阶段进行此操作,但是想知道是否有一种更简单的方法使用所需的HTML的模板来执行此操作?
我已经看到了使用parentElement.innerhtml(variablename)的大量示例,其中variablename是模板字面的字面形式,但是下面说明的这些按钮元素在循环内,并希望我在parent .board中预先将新创建的按钮预留到parent
.board -LIST
html中显示的元素。
提交新的板名称时,在后台发生了一个fetch()邮政请求以更新数据库,但是我需要使用JavaScript创建一个新元素,因此这立即向用户显示。
目前,模板文字newbutton
被添加到引用标记的HTML中,作为文本字符串,而不是HTML DOM元素。
javaScript
// added into the template literal below
const newBoardName = document.querySelector('.input-title').value;
const newButton = `
<button class="board-list-item full-width" name="board-name" type="submit">
<span>${newBoardName}</span>
<span class="add-icon flex">+</span>
</button>
`
document.querySelector(".board-list").prepend(newButton);
html
<div class="board-list">
// buttons outputted from the database appear here
</div>
<form>
<input class="input-title">
<button name="new-board-name">New Board Name</button>
<form>
I have a loop of button elements that are outputted with a while
loop from data called from a MySQL database via PHP.
A user can add a button to this list and I want to add the new button and it's associated HTML using the prepend()
method on the parent element, so it appears at the top of the list.
I know how to do this in various stages using createElement and adding class names and attribute names, but wondered if there is a simpler way of doing it using a template literal of the required HTML?
I've seen plenty of examples using parentElement.innerHTML(variableName), where variableName is the template literal, but these button elements illustrated below are inside a loop, and want I to prepend the newly created button to the parent .board-list
element shown in the HTML.
When a new board name is submitted, a fetch() post request happens in the background to update the database, but I need to create a new element with JavaScript so this shows instantly to the user.
At the moment the template literal newButton
is added to the HTML inside quote marks as a string of text, not as HTML DOM elements.
JavaScript
// added into the template literal below
const newBoardName = document.querySelector('.input-title').value;
const newButton = `
<button class="board-list-item full-width" name="board-name" type="submit">
<span>${newBoardName}</span>
<span class="add-icon flex">+</span>
</button>
`
document.querySelector(".board-list").prepend(newButton);
HTML
<div class="board-list">
// buttons outputted from the database appear here
</div>
<form>
<input class="input-title">
<button name="new-board-name">New Board Name</button>
<form>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为一个简单的解决方案是使用
.innerhtml
,这是一个示例:这仅仅是为了回答您的问题,尽管它不是最好的解决方案,所以我认为它没有推荐。
I think a simple solution is to use
.innerHTML
, here is an example:This is simply to answer your question, although it is not the best solution, so I do not see it recommended.
解决方案是使用
insertadjacenthtml
方法。其中一条评论中给出的问题/答案对我有所帮助,但我认为这不是一个重复的问题,而与之相关的问题有一个过于复杂的答案IMHO。The solution to this was using the
insertAdjacentHTML
method. The question/answer given in one of the comments helped me on this, but I don't think it is a duplicate question, and the question linked to has an overly complicated answer IMHO.