2016-09-30 74 views
0

我需要一些Javascript和CSS幫助。我有一個工作正常的腳本,但是當我在頂部執行過濾功能時,我的CSS將不會運行,只有過濾後的結果將顯示在HTML頁面中。在執行JavaScript時尋求建議。運行javascript無法運行CSS

<script> 
     var createTable = function(){ 
     var street = document.getElementById("search-street").value; 
     var suburb = document.getElementById("search-suburb").value; 
     var pc = document.getElementById("search-pc").value; 
     var type = document.getElementById("search-type").value; 
     var counter=0; 
     if (street == '' && suburb == '' && pc == '' && type == '') { 
     for(i=0;i<myArray.length;i++){ 
      var id = myArray[i][4]; 
      if(counter%3==0){ 
      document.write("<div class='row'>"); 
      } 
      document.write("<div class='col-sm-6 col-md-4'>"); 
      document.write("<div class='thumbnail'>"); 
      document.write("<div class='caption'>"); 
      document.write("<p>" + myArray[i][3] + "</p>"); 
      document.write("<p><a href='edit_property.php?id="+id+"' class='btn btn-default' role='button'>Edit</a> <a href='#delete_property.php?id="+id+"' class='btn btn-danger' role='button'>Delete</a></p>"); 
      document.write("</div>"); 
      document.write("</div>"); 
      document.write("</div>"); 
      counter++; 
      if(counter%3==0){ 
      document.write("</div>"); 
      } 
     } 
     } else { 
     for(i=0;i<myArray.length;i++){ 
      var id = myArray[i][4]; 
      if(counter%3==0){ 
      document.write("<div class='row'>"); 
      } 
      if(street==myArray[i][0] || suburb==myArray[i][1] || pc==myArray[i][2] || type==myArray[i][3]){ 
      document.write("<div class='col-sm-6 col-md-4'>"); 
      document.write("<div class='thumbnail'>"); 
      document.write("<div class='caption'>"); 
      document.write("<p>" + myArray[i][3] + "</p>"); 
      document.write("<p><a href='edit_property.php?id="+id+"' class='btn btn-default' role='button'>Edit</a> <a href='#delete_property.php?id="+id+"' class='btn btn-danger' role='button'>Delete</a></p>"); 
      document.write("</div>"); 
      document.write("</div>"); 
      document.write("</div>"); 
      counter++; 
      } 
      if(counter%3==0){ 
      document.write("</div>"); 
      } 
     } 
     } 
    } 
    createTable(); 
    </script> 
+0

哪裏CSS?你是什​​麼意思,「CSS不會運行」? – nnnnnn

+0

我會考慮使用比20世紀90年代的'document.write'更先進的技術 - 說過,你說你的'CSS不會運行' - 並且你還沒有發佈任何CSS –

回答

0

請勿使用document.write,使用變量並替換或添加元素內的內容。

試試這個小提琴:https://jsfiddle.net/cq14qot3/

var myArray = [["1", "2", "3", "4", "5"],["6", "7", "8", "9", "10"]]; 
    var createTable = function() { 
     var street = document.getElementById("search-street").value; 
     var suburb = document.getElementById("search-suburb").value; 
     var pc = document.getElementById("search-pc").value; 
     var type = document.getElementById("search-type").value; 
     var counter = 0; 
     var outputHTML = ''; 
     if (street == '' && suburb == '' && pc == '' && type == '') { 
      for (i = 0; i < myArray.length; i++) { 
       var id = myArray[i][4]; 
       if (counter % 3 == 0) { 
        outputHTML += ("<div class='row'>"); 
       } 
       outputHTML += ("<div class='col-sm-6 col-md-4'>"); 
       outputHTML += ("<div class='thumbnail'>"); 
       outputHTML += ("<div class='caption'>"); 
       outputHTML += ("<p>" + myArray[i][3] + "</p>"); 
       outputHTML += ("<p><a href='edit_property.php?id=" + id + "' class='btn btn-default' role='button'>Edit</a> <a href='#delete_property.php?id=" + id + "' class='btn btn-danger' role='button'>Delete</a></p>"); 
       outputHTML += ("</div>"); 
       outputHTML += ("</div>"); 
       outputHTML += ("</div>"); 
       counter++; 
       if (counter % 3 == 0) { 
        outputHTML += ("</div>"); 
       } 
      } 
     } else { 
      for (i = 0; i < myArray.length; i++) { 
       var id = myArray[i][4]; 
       if (counter % 3 == 0) { 
        outputHTML += ("<div class='row'>"); 
       } 
       if (street == myArray[i][0] || suburb == myArray[i][1] || pc == myArray[i][2] || type == myArray[i][3]) { 
        outputHTML += ("<div class='col-sm-6 col-md-4'>"); 
        outputHTML += ("<div class='thumbnail'>"); 
        outputHTML += ("<div class='caption'>"); 
        outputHTML += ("<p>" + myArray[i][3] + "</p>"); 
        outputHTML += ("<p><a href='edit_property.php?id=" + id + "' class='btn btn-default' role='button'>Edit</a> <a href='#delete_property.php?id=" + id + "' class='btn btn-danger' role='button'>Delete</a></p>"); 
        outputHTML += ("</div>"); 
        outputHTML += ("</div>"); 
        outputHTML += ("</div>"); 
        counter++; 
       } 
       if (counter % 3 == 0) { 
        outputHTML += ("</div>"); 
       } 
      } 
     } 
     document.body.innerHTML = outputHTML; 
     alert(outputHTML); 
    } 
    createTable(); 
+0

盲目覆蓋'document.body.innerHTML'可能不會產生OP期望的確切結果:p –

+0

但它足夠接近「document.write」xD –

+0

如果您添加CSS,它將始終「保持」,在這裏更新小提琴:https://jsfiddle.net/9ov8cLkb/ –