使用jquery将内容附加到div

时间:2016-11-04 13:23:56

标签: jquery

我尝试使用jQuery函数将内容附加到div。如果我手动指定div的id,它会被添加。如果我将id指定为函数的参数,则它不起作用。

function addcontent(TargetDiv) {
  $(TargetDiv).append('Some text here');
}

$(document).ready(function() {
  addcontent('divTable');
});
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css" rel="stylesheet" />

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

<div id="divTable"></div>
hai

1 个答案:

答案 0 :(得分:2)

您需要在ID前面添加#才能找到它:

function addcontent(TargetDiv)
{

  $("#" + TargetDiv).append('Some text here');        

}