2015-12-03 28 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>"); 
    }); 
    });