2017-04-17 75 views
0

我是JavaScript新手,如果我没有解释我想要的东西,请耐心等待。所以基本上我有一个创建活动的表单,所以每当用户填充字段并点击保存时,它将创建一个包含信息的div实例。现在这些信息基本上是一致的,我想组织它们在一个盒子(div)中,但我不知道如何创建它,它使用JS还是HTMl?如何在一个盒子内包装一个div的实例

下面是我现在正在工作的图片。 enter image description here

这里是代码视图。

window.onload = function() { 
function addAct(nameact, when, where,desc) { 
var buses = document.querySelectorAll(".bus"); 
var curr = document.createElement("div"); 
curr.setAttribute("class", "name"); 
var p0 = document.createElement("p"); 
p0.setAttribute("class", "nameact"); 
p0.innerHTML = nameact; 
var p1 = document.createElement("p"); 
p1.setAttribute("class", "whereisit"); 
p1.innerHTML = when; 
var p2 = document.createElement("p"); 
p2.setAttribute("class", "when"); 
p2.innerHTML = where; 
var p3 = document.createElement("p"); 
p3.setAttribute("class", "describtion"); 
p3.innerHTML = desc; 
curr.appendChild(p0); 
curr.appendChild(p1); 
curr.appendChild(p2); 
curr.appendChild(p3); 
if (buses.length) { 
    buses[buses.length -1].insertAdjacentHTML("afterEnd", curr.outerHTML) 
} else { 
    document.forms[1].insertAdjacentHTML("afterEnd", curr.outerHTML) 
} 
} 

var obj = {nameact: "", when: "", where: "",desc:""}; 

document.forms[1].onchange = function(e) { 
obj[e.target.name] = e.target.value; 
} 

document.forms[1].onsubmit = function(e) { 
e.preventDefault(); 
addAct(obj.nameact, obj.when, obj.where, obj.desc) 
} 
} 

<section id="cd-placeholder-2" class="cd-section cd-container"> 
     <h2>Activities</h2>  
     <div id="Slider" class="slide-up"> 
      <div> 
       <div class="contents" > 


     <form class="form2"> 

         <div class="col-3"> 
         <label> 
          Activity Name 
          <input placeholder="What is your activity?" tabindex="3" name="nameact" /> 
         </label> 
         </div> 

         <div class="col-3"> 
         <label> 
          Where? 
          <input placeholder="Where is it going to be?" tabindex="4" name="when" /> 
         </label> 
         </div> 

         <div class="col-3"> 
         <label> 
          When? 
          <input type="date" placeholder="mm\dd\yy" tabindex="4" name="where"/> 
         </label> 
         </div> 

         <div> 
         <label> 
          Care to share more? 
          <textarea placeholder="describtion of your activity" class="text" name="desc"></textarea> 
         </label> 
         </div> 

         <button type="submit" value="Submit" class="cd-btn" id="col-submit" style="position:relative;float:right;overflow:hidden;margin:10px;margin-top:30px;">Add Activity</button> 


     </form> 

        <div id="name"></div> 

       </div> 
      </div> 
     </div> 

    </section> 

任何反馈是高度赞赏。

+1

你真正需要的是CSS。 – durbnpoisn

+0

你能告诉我如何处理它吗? –

+0

@ MaryamAl-Mansour你可以检查我的答案。如果不起作用,请发表评论。如果它有效,请'接受为答案' –

回答

1

您需要HTML + CSS + JS

HTML:

<div id="name"></div> 

CSS

.row{ 
    background-color: red; 
    border: 5px solid #333; 
    display: block; 
    position: relative; 
    overflow: hidden; 
} 
.nameact{ 
    background-color:red; 
    height: 40px; 
    display: inline-block; 
} 
.whereisit{ 
    background-color:green; 
    height: 40px; 
    display: inline-block; 
} 
.when{ 
    background-color:blue; 
    height: 40px; 
    display: inline-block; 
} 
.describtion{ 
    background-color:brown; 
    height: 40px; 
    display: inline-block; 
} 

JS

var obj = {nameact: "", when: "", where: "",desc:""}; 

document.forms[1].onchange = function(e) { 
obj[e.target.name] = e.target.value; 
} 

document.forms[1].onsubmit = function(e) { 
e.preventDefault(); 
addAct(obj.nameact, obj.when, obj.where, obj.desc) 
} 

function addAct(nameact, when, where,desc) { 
var myhtml = '<div class="row"><div class="nameact">'+nameact+'</div><div class="whereisit">'+where+'</div><div class="when">'+when+'</div><div class="describtion">'+describtion+'</div></div>'; 

document.getElementById('name').innerHTML += myhtml; 

} 

您将需要更新的CSS代码的基础上,您的需求

+0

谢谢,它的工作,但它仍然不会创建一个信息框或分区。 –

+0

@ MaryamAl-Mansour它实际上创造了箱子,但你无法看到它。现在我更新了css代码,你也应该更新你的。我想你会意识到你必须做的。剩下的部分是关于CSS,我想你应该研究它。 –

+0

@ MaryamAl-Mansour kyle上面的答案是您可以用CSS做什么的一个很好的例子。 –

0

如果您需要任何帮助了解这一点,请让我知道。我写了几个CSS样式,以及它们将应用于的HTML标记。让你的JavaScript产生这个标记,并做一些关于CSS的阅读,将其推断为一个完整的模块。

<style> 
    .activity { 
     width: 645px; 
     height: 160px; 
     border: 2px solid #0000ff; 
    } 

    .activity > div { 
     padding: 20px; 
    } 

    .activity .top { 
     display: inline-block; 
     float: left; 
     width: 174px; 
     height: 30; 
     border-right: 1px solid #0000ff; 
    } 

    .activity .top.end { 
     width: 175px; 
     border-right: none; 
    } 

    .activity .bottom { 
     display: inline-block; 
     float: left; 
     width: 605px; 
     height: 50; 
     border-top: 1px solid #0000ff; 
    } 

    .activity .title { 
     display: block; 
    } 


</style> 


<div class="activity"> 
    <div class="top"> 
     <span class="title">ACTIVITY NAME</span> 
     <span>Skydiving</span> 
    </div> 

    <div class="top"> 
     <span class="title">WHERE?</span> 
     <span>Dubai Palm Island</span> 
    </div> 

    <div class="top end"> 
     <span class="title">2017-06-22</span> 
    </div> 

    <div class="bottom"> 
     <span>CARE TO SHARE MORE?</span> 
     <span class="title">It will be a fun trip!</span> 
    </div> 
</div> 

我希望这能让你朝着正确的方向前进!

相关问题