Javascript Div元素问题

时间:2013-07-08 19:52:19

标签: javascript html html5 web scripting

好的,所以我有一堆HTML div类和一个包含所有类的ID。我需要在Javascript中使用相同的概念来生成相同的结果。任何帮助将不胜感激 :)。我的主要问题是我无法获得我编写的JavaScript代码以与我编写的HTML相同的方式显示CSS。

HTML:

<body>

<div id = "barOne">
<div class = standard> 
<p> Magazine </p>
</div>
<div class = standard>
<p> Newspaper </p>
</div>
<div class = standard>
<p> English </p>
</div>
<div class = standard>
<p> Friends </p>
</div>
<div class = standard>
<p> Photos </p>
</div>
<div class = standard>
<p> Videos </p>
</div>
<div class = standard>
<p> Admin </p>
</div>
</div> <!-- End barOne-->

</body>

这是我尝试将代码转换为JavaScript,我对缺少什么的想法?

function mainMenuBar() {
console.log("Mainmenubar invoked...");

var menuBarWrapper = document.createElement("div");
var btns = [];
var btnTxt = [];
for (var i = 0; i < 6; i++) {
btns.push(document.createElement("div"));//Adds a button
btnTxt.push(document.createElement("p"));//Adds a p tag

btns[i].className = "standard";
btns[i].id = "btn" + i;
//btns[i].setActive("onclick","clickFunction()");
//btnTxt[i].className = "";


btns[i].appendChild(btnTxt);//Adds the lbl to button
menuBarWrapper.appendChild(btnTxt[i]);

}
document.getElementById("document.body").appendChild(menuBarWrapper);
}
function clickFunction() {
console.log("clickFunc invoked");
}

这是我想要与当前使用上面显示的HTML代码呈现的JavaScript代码对应的CSS ...

body{
background-color: f5f5f5;   
}
.canvasGeneral {
height: 768px;
width: 1024px;
position:fixed;
left:50%;
background-color: f5f5f5;
height:768px;
/*overflow:hidden;*/
}

#barOne {
border-top: 1px solid #939393; 
left: 0px;
width: 1024px;
bottom: 1px;
height 39px; 
position:absolute;
padding-left: 35px;
/*Quick nav bar at the bottom, primary nav*/
}

.standard {
margin: 9px; 
background-color:f5f5f5;
-moz-border-radius:4px;
-webkit-border-radius:4px;
border-radius:4px;
display:inline-block;
color: #939393;
font-family:helvetica;
font-size:13px;
font-weight:light;
padding:0px 33px;
text-decoration:none;
 -webkit-transition: 0 .2s;
   -moz-transition: 0s .2s;
    -ms-transition: 0 .2s;
     -o-transition: 0 .2s;
        transition: 0 .2s;
}
.standard:active {
position:relative;
color: #007bff;
 -webkit-transition: none;
   -moz-transition: none;
    -ms-transition: none;
     -o-transition: none;
        transition: none;
}

3 个答案:

答案 0 :(得分:1)

你能发布失败的帖子吗?你错过了每个div的类名吗?它是否插入了div?

你有点做一些没有意义的事情。每个div创建一个块,因为所有嵌套的div都具有相同的类,并且外部类围绕它们所有,为什么不组合这两个类并且有一个div块或每个单独的div块。有没有理由为什么你这样做?

答案 1 :(得分:0)

你可以这样做...... Live Demo

对您的代码进行了一些更改......

<强>被修改!

function mainMenuBar() {
    console.log("Mainmenubar invoked...");

    var menuBarWrapper = document.createElement("div"),
        text = ["Magazine", "Newspaper", "English", "Friends", "Photos", "Videos", "Admin"],
        ele = null,
        i = 0;

    menuBarWrapper.id = "barOne";

    for (i = 0; i < 7; i++) {
        ele = document.createElement("div");
        ele.className = "standard";
        ele.id = "btn" + i;
        ele.innerHTML = "<p>" + text[i] + "</p>";
        menuBarWrapper.appendChild(ele);
    }
    // instead of adding event listener per element would be better if you use event delegation
    menuBarWrapper.addEventListener("click", clickFunction, false); 
    document.getElementsByTagName("body")[0].appendChild(menuBarWrapper);
}

function clickFunction(e) {
    if (e.target.nodeName.toLowerCase() === 'p') {
        console.log("clickFunc invoked");
        e.stopPropagation();
        e.preventDefault();
    }
}

mainMenuBar();

答案 2 :(得分:0)

好的,这是代码:

console.log("Mainmenubar invoked...");

var menuBarWrapper = document.createElement("div");
    menuBarWrapper.id = 'barOne';
var btns = [];
var btnTxt = [];
for (var i = 0; i < 6; i++) {
    btns.push(document.createElement("div"));//Adds a button
    btnTxt.push(document.createElement("p"));//Adds a p tag
    btnTxt[i].innerHTML = 'button ' + i; // Added this line just to show the element
    btns[i].id = "btn" + i;
    btns[i].addEventListener("click", clickFunction);

    btns[i].appendChild(btnTxt[i]);//Adds the lbl to button
    menuBarWrapper.appendChild(btns[i]);

}
document.body.appendChild(menuBarWrapper);
console.log(document.body.innerHTML);

function clickFunction() {
    console.log("clickFunc invoked");
}

然后只需将.standard选择器替换为#oneBar div,将.standard:active替换为#oneBar div:active

A demo at jsFiddle