【问题标题】:Place one HTML input directly below another with HTML使用 HTML 将一个 HTML 输入直接放在另一个 HTML 输入下方
【发布时间】:2013-04-29 21:11:47
【问题描述】:

所以我有一堆动态创建的 HTML 元素。我真的希望元素“imageBox”直接出现在“mediaBox”下方,而不是像现在这样出现在它的左侧。这是我现在的代码:

var div = document.createElement("div");
//HTML code for each element to be added with each new checkpoint
var nameBox = "<b>Checkpoint " + markerId + ":</b> <input type='text' id=" + markerId + " placeholder='Checkpoint name'>"
var descBox = "<textarea rows='4' cols='35' placeholder='Checkpoint description' id=" + markerId + "desc style='vertical-align: top;'></textarea>"
var mediaBox = "<input type='text' id='media" + markerId + "' placeholder='paste URL to YouTube video' size='23'>"
var imageBox = "<input type='text' id='image" + markerId + "' placeholder='paste URL to image' size='23'>"
var removeButton = "<button type='button' value='Remove' id='remove" + markerId + "' onClick='remove_marker(" + markerId + ")'> Remove </button>"
var undoButton = "<button type='button' value='Undo' ' style='display: none;' id='undo" + markerId + "' onClick='remove_marker(" + markerId + ")'> Undo </button>"
var removeText = "<div id='removed" + markerId + "' style='display: none;'> Removed <button type='button' value='Undo' ' style='display: none;' id='undo" + markerId + "' onClick='undo_Remove(" + markerId + ")'> Undo </button> </div>" 
div.innerHTML = nameBox + descBox + mediaBox + imageBox + removeButton + removeText;

我知道这真的很混乱,理想情况下我应该使用 CSS,但我现在不是。它在我的页面上显示如下:

如果图片 URL 框可以在 YouTube 的正下方,那就太好了!

非常感谢!

【问题讨论】:

  • 如果你知道应该使用 CSS,为什么不呢?
  • 说实话,没有 CSS 我不相信你能做到这一点。我的意思是你可以找到一个预构建的 css 模块来执行此操作,但你肯定需要一些 CSS。如果你愿意,你可以在 html 元素中内联。

标签: html input formatting styles


【解决方案1】:

为了尽量减少重写,您可以尝试将两个框包装在一个无序列表中。如果您真的想避免使用 CSS,请设置内联样式以删除项目符号。

div.innerHTML = (nameBox 
                 + descBox 
                 + '<ul style="list-style: none;"><li>' 
                 + mediaBox + "</li><li>" + imageBox 
                 + "</li></ul>" 
                 + removeButton 
                 + removeText);

我刚刚将相关位插入您的最后一行。你真的应该使用document.createElementdiv.appendChild 来创建这些东西。

【讨论】:

  • 这似乎打破了它。我对这些东西还不够熟悉,无法弄清楚原因。
  • 这可能是&lt;ul style... 部分中的错误引号。我已将外部引号更改为单引号
  • 嗯,这似乎将两个框放在描述框的正下方而不是它旁边。
猜你喜欢
  • 2017-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多