2015-12-03 34 views
1

我試圖在動態生成的表後添加一個跨度,但跨度是在表的內容正在加載之前添加的。從動態生成的表後,從jQuery插入跨度

下面是我的代碼:

<!DOCTYPE html> 
<html> 
<head> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> 
<script> 
$(document).ready(function(){ 
    dash(); 
    $("button").click(function(){ 
     $("#th").append("<span>span</span>"); 
    }); 
}); 

function dash() 
{ 
    $("#hu").html(); 
    $("#hu").append("<tr> <td> see this </td> </tr>"); 
} 
</script> 
</head> 
<body> 

<button>Insert content after each p element</button> 
<div align="right" id="th"> 
<table id="hu" align="right"> 
<tr><td>hello</td></tr> 
</table> 
</div> 
</body> 
</html> 

跨度是越來越你好前加入。我希望跨度顯示在表格內容之後。

+0

如果此鏈接hello' – guradio

+0

後我出現'是什麼'$目的( 「#胡」)HTML();'在功能上破折號() – Reoxey

+0

不允許表使用align =「right」 – Afsar

回答

1

的跨度在DOM底部添加後,像這樣$("#hu").after("<span>span</span>");

這將使跨度。表之前,你也都看到了,因爲表對準right.Try如下:

$(document).ready(function(){ 
 
    dash(); 
 
    $("button").click(function(){ 
 
     $("#th").append("<span>span</span><br/>"); 
 
    }); 
 
}); 
 

 
function dash() 
 
{ 
 
    $("#hu").html(); 
 
    $("#hu").append("<tr> <td> see this </td> </tr>"); 
 
}
<!DOCTYPE html> 
 
<html> 
 
<head> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> 
 

 
</head> 
 
<body> 
 

 
<button>Insert content after each p element</button> 
 
<div align="right" id="th"> 
 
<table id="hu" > 
 
<tr><td>hello</td></tr> 
 
</table> 
 
    <br/> 
 
</div> 
 
</body> 
 
    
 
</html>

1

你寫表

+1

確實,但這並不能解決他所面臨的問題。你的建議是做完全相同事情的另一種方式。 –

1

使用jQuery after功能喜歡的方式@Reoxey一樣。但是,請確保您在</body>標記之前添加了腳本,而不是在<head>標記上使用它。建議在</body>標記之前添加Javascript,以防止在腳本加載時呈現阻塞,並且對站點感知頭更好。在</body>標籤之前添加Javascript也將提高網站加載速度。

1

嘿馬努試試這個。

<html> 
<head> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> 
<script> 
$(document).ready(function(){ 
    dash(); 
    $("button").click(function(){ 
     $("#hu").append("<span>span</span>"); 
    }); 
}); 

function dash() 
{ 
    $("#hu").html(); 
    $("#hu").append("<tr> <td> see this </td> </tr>"); 
} 
</script> 
</head> 
<body> 

<button>Insert content after each p element</button> 
<div align="right" id="th"> 
<table id="hu" align="right"> 
<tr><td>hello</td></tr> 
</table> 
</div> 
</body> 
</html> 

應使用表ID即#hu追加爲地方#th

希望這有助於中。 :)

1

http://www.discussdesk.com/add-remove-more-rows-dynamically-using-jquery.htm

請通過上面的鏈接和代碼像下面.....

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> <script type="text/javascript"> 
var rowCount = 1; 
function addMoreRows(frm) { 
rowCount ++; 
var recRow = '<p id="rowCount'+rowCount+'"><tr><td><input name="" type="text" size="17%" maxlength="120" /></td><td><input name="" type="text" maxlength="120" style="margin: 4px 5px 0 5px;"/></td><td><input name="" type="text" maxlength="120" style="margin: 4px 10px 0 0px;"/></td></tr> <a href="javascript:void(0);" onclick="removeRow('+rowCount+');">Delete</a></p>'; jQuery('#addedRows').append(recRow); } function removeRow(removeNum) { jQuery('#rowCount'+removeNum).remove(); } </script> - See more at: http://www.discussdesk.com/add-remove-more-rows-dynamically-using-jquery.htm#sthash.vrG6CPEc.dpuf 
0

這將幫助你

$(document).ready(function(){ 
    dash(); 
    $("button").click(function(){ 
     $("#hu").after("<span>span</span>"); 
    }); 
    });