0

好的,所以我有一堆 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;
}
4

3 回答 3

1

您可以发布失败的内容吗?您是否缺少每个 div 的类名?它是否插入了div?

不过,您正在做一些没有意义的事情。每个 div 都会创建一个块,因为所有嵌套的 div 都具有相同的类,而外部类围绕着它们,为什么不将这两个类结合起来,并有一个 div 块或每个单独的 div 块。你这样做有什么原因吗?

于 2013-07-08T20:10:34.780 回答
0

你可以做这样的事情...... 现场演示

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

已编辑!

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();
于 2013-07-08T21:35:33.793 回答
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

jsFiddle 的演示

于 2013-07-08T22:12:22.540 回答