2016-04-25 56 views
0

嗨,所以我认为这可能是一个非常简单的问题,但我无法找到答案的任何地方。JavaScript到表中的一个单元格而不是整个页面

我有一个JavaScript函数,它从我的数据库拉动信息,我试图让它成为成一个表格的一个单元,但它输出到整个页面

what the system is outputting

的HTML

<tr> 
<td class="tg-baqh" id='Emp1'></td> 
<td class="tg-yw4l"><input type="text" placeholder="enter shift" class="form-control" id='shiftInput'></td> 
<td class="tg-yw4l"><input type="text" placeholder="enter shift" class="form-control" id='shiftInput'></td> 
<td class="tg-yw4l"><input type="text" placeholder="enter shift" class="form-control"></td> 
<td class="tg-yw4l"><input type="text" placeholder="enter shift" class="form-control"></td> 
<td class="tg-yw4l"><input type="text" placeholder="enter shift" class="form-control"></td> 
<td class="tg-yw4l"><input type="text" placeholder="enter shift" class="form-control"></td> 
<td class="tg-yw4l"><input type="text" placeholder="enter shift" class="form-control"></td> 
<td class="tg-yw4l">30/16</td> 

JavaScript的

 $(function Emp1(){ 

    // Get a database reference to our posts 
    var employeeref = new Firebase("https://shiftsapp.firebaseio.com/employees/emp1"); 

    // Attach an asynchronous callback to read the data at our posts reference 
    employeeref.on("value", function(snapshot) { 
    console.log(snapshot.val()); 
    document.write(snapshot.val()); 
    }, function (errorObject) { 
     console.log("The read failed: " + errorObject.code); 
     }); 

     }); 
+1

这就是'document.write'的作用,它会覆盖文档? – adeneo

+0

您需要从这里删除图像,并将代码本身作为问题的一部分发布。 – ManoDestra

+1

@ManoDestra我已经添加了代码,并带走了图像。 – kelliehughesspare

回答

1

执行document.write()方法替换整个文档元素,而不是你应该使用document.getElementByIddocument.getElementByClassName或某些元素选择方法,然后replaceappend的HTML只为元素。如果你使用的是JQuery,你可以这样做:

var resutlHtml = "<div>test</div>"; //this is your html response 
$('#tableId .tablecellclass').html(resultHtml); 
相关问题