简体   繁体   English

将HTML标记添加到由jQuery添加的div

[英]Add HTML markup to div that is added by jQuery

I have an onclick function that adds HTML markup (a table) to a specific div with an id attribute. 我有一个onclick函数,它将HTML标记(一个表)添加到具有id属性的特定div。

Example: 例:

$(‘#someDiv’).html(‘<table id=“userTable”><\table>’);

I want to create another function that adds HTML markup to the markup I already added via jQuery. 我想创建另一个函数,将HTML标记添加到我已经通过jQuery添加的标记中。

Example: 例:

$(‘#userTable’).html(‘<tr><td>Hello<\td><\tr>’);

But, the computer didn't know the id “userTable” and adds nothing to the table . 但是,计算机并不知道id “用户表”,并增加了没什么好table

How can I access this table from another function and add to its HTML markup? 如何从其他函数访问此table并添加到其HTML标记?

I'm not sure if "userTable" is a static or dynamic ID. 我不确定“userTable”是静态ID还是动态ID。 This is what I understand you need 这是我理解你需要的

  $(function(){ $("#btnAddRow").hide(); $("#btnAddTable").click(function(){ let targetDiv = $("#targetDiv"), new_table = $("" + "<table border='1' id='userTable'>" + "<thead>" + " <tr>" + " <th>Column 1</th>" + " <th>Column 2</th>" + " </tr>" + "</thead>" + "<tbody></tbody>" + "</table>"); targetDiv.append(new_table); $("#btnAddRow").show(); }); $("#btnAddRow").click(function(){ let targetDiv = $("#targetDiv"), userTable = targetDiv.find("#userTable"), new_tr = $("" + "<tr>" + " <td>Something</td>" + " <td>Something Else</td>" + "</tr>"); userTable.find("tbody").append(new_tr); }); }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <button type="button" id="btnAddTable">Add Table</button> <button type="button" id="btnAddRow">Add Row</button> <div id="targetDiv"> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM