Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用jquery/javascript设置嵌套div的样式_Javascript_Jquery_Html_Css - Fatal编程技术网

使用jquery/javascript设置嵌套div的样式

使用jquery/javascript设置嵌套div的样式,javascript,jquery,html,css,Javascript,Jquery,Html,Css,我正在尝试用javascript创建嵌套的div,但没有成功。下面是我尝试的代码。我正在应用外部css <!DOCTYPE html> <html> <!--<meta charset="UTF-8">--> <meta content="text/html; charset=ISO-8859-1" http-equiv="content-type"> <title>first2</title>

我正在尝试用javascript创建嵌套的div,但没有成功。下面是我尝试的代码。我正在应用外部css

<!DOCTYPE html>
 <html>
   <!--<meta charset="UTF-8">-->
  <meta content="text/html; charset=ISO-8859-1" http-equiv="content-type">
<title>first2</title>
  <head>
   <link rel="stylesheet" type="text/css" href="jsoncss.css">
 </head>
   <script type="text/javascript"             src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
   <script>

     /*html=$("<div class='wrapper'>").append(
         $("<div class='LeftSection'>").append(
              $("<div class='MyMainSavings'>")).append(
                               $("<div class='MyMainSavingsTop'>")));*/


      /*$("<div>", {class: "wrapper"}).append(
    $("<div>", {class: "LeftSection"}).append(
        $("<div>", {class: "MyMainSavings"}).append(
            ("<div>", {class: "MyMainSavingsTop"}),
            ("<div>", {class: "MyMainSavingsBottom"})
        ),
        $("<div>", {class: "MyMainSavings"}).append(
            ("<div>", {class: "MyMainSavingsTop"}),
            ("<div>", {class: "MyMainSavingsBottom"})
        ),
        $("<div>", {class: "MyMainSavings"}).append(
            ("<div>", {class: "MyMainSavingsTop"}),
            ("<div>", {class: "MyMainSavingsBottom"})
        ),
        $("<div>", {class: "MyMainSavings"}).append(
            ("<div>", {class: "MyMainSavingsTop"}),
            ("<div>", {class: "MyMainSavingsBottom"})
        )
    ), 
    $("<div>", {class: "RighSection"}).append(
        $("<span>").text(
            "Some text"
        )
    )
     ).appendTo("body")*/

  var iDiv = document.createElement('div');

    //iDiv.id = 'wrapper';
    iDiv.className = 'wrapper';
     //document.getElementsByTagName('body')[0].appendChild(iDiv);

      var div2 = document.createElement('div');
          div2.className = 'LeftSection';
     iDiv.appendChild(div2);    

     /*$(document).ready(function () {
         $(".wrapper").addClass("wrapper");
         });*/
      </script>
     <body> 
     </body>
      </html>

前2名
/*html=$(“”)。追加(
$(“”)。附加(
$(“”)。附加(
$("")));*/
/*$(“”,{class:“包装器”})。追加(
$(“”,{class:“LeftSection”})。追加(
$(“”,{class:“MyMainSavings”})。追加(
(“”,{class:“MyMainSavingsTop”}),
(“”,{class:“MyMainSavingsBottom”})
),
$(“”,{class:“MyMainSavings”})。追加(
(“”,{class:“MyMainSavingsTop”}),
(“”,{class:“MyMainSavingsBottom”})
),
$(“”,{class:“MyMainSavings”})。追加(
(“”,{class:“MyMainSavingsTop”}),
(“”,{class:“MyMainSavingsBottom”})
),
$(“”,{class:“MyMainSavings”})。追加(
(“”,{class:“MyMainSavingsTop”}),
(“”,{class:“MyMainSavingsBottom”})
)
), 
$(“”,{class:“RighSection”})。追加(
$(“”)。文本(
“一些文本”
)
)
).附件(“正文”)*/
var iDiv=document.createElement('div');
//iDiv.id='包装器';
iDiv.className='wrapper';
//document.getElementsByTagName('body')[0].appendChild(iDiv);
var div2=document.createElement('div');
div2.className='LeftSection';
iDiv.附属儿童(第2分部);
/*$(文档).ready(函数(){
$(“.wrapper”).addClass(“wrapper”);
});*/
如何将外部css链接到javascript中创建的div?我尝试了addClass,但不起作用。
请考虑注释代码,这些是我尝试过的不同想法。

在将元素追加到主标记之后,可以应用其他事件,

var p=document.getElementById('ptag');
var div=document.createElement('div');
div.className='wrapper';
var div1=document.createElement('div');
div1.id='wrapper';
儿童分部(第1分部);

p、 儿童组;// 我在脚本中看到的唯一问题是您没有将创建的IDiv附加到DOM中的任何其他元素

最后,您需要像下面这样附加它

var iDiv = document.createElement('div');

    //iDiv.id = 'wrapper';
    iDiv.className = 'wrapper';
     //document.getElementsByTagName('body')[0].appendChild(iDiv);

      var div2 = document.createElement('div');
          div2.className = 'LeftSection';
     iDiv.appendChild(div2);    
    document.getElementById('any-parent-element-id').appendChild(iDiv)
这可能是一种方法

html='<div class="element"><p class="children"></p></div>'; 

$('#wrapper').html(html);
html='

; $('#wrapper').html(html);
您发布的代码有一半以上被注释掉。如果这不相关,为什么要发布它?它只会增加混乱,使人们的注意力从手头的问题上移开。不要偷懒,把你的代码当着别人的面扔。负责并在输入问题时发布整洁的代码。请阅读本文——恕我直言,我发布了这些代码,以展示我尝试过的所有想法。没关系。但是,您需要清楚地解释您的问题并清理代码。有些尝试使用jQuery,有些尝试使用纯Javascript。请清理您的代码,大多数注释代码不适用。明确指定您想要做什么,您尝试的代码应该反映这一点。当你发布一个更好的问题时,你会得到更好的答案。你能帮我把代码弄好吗?您认为在脚本标记之后开始的代码怎么样?这就是您所需要的全部内容--