ZBLOG

for (const item of jsonRes) { if (item.code === 'customerInfo') {} //客户信息展示 if (item.code === 'loan') { // 合同展示 const buttonContainer = document.createElement('div'); buttonContainer.innerHTM...

要将按钮容器添加到页面中的特定位置,而不是默认的底部,您需要确定该元素应该插入到哪个具体的位置。假设您想将按钮容器添加到某个特定的父元素中,您可以使用 insertBeforeappendChild 方法来控制位置。

以下是一个示例代码,它演示了如何将按钮容器插入到一个指定的父元素中(例如,一个具有特定 ID 的 div 元素):

  1. for (const item of jsonRes) {
  2. if (item.code === 'customerInfo') {
  3. // 客户信息展示
  4. }
  5. if (item.code === 'loan') { // 合同展示
  6. // 找到目标父元素(假设它的ID是 "targetContainer")
  7. const targetContainer = document.getElementById('targetContainer');
  8. // 创建按钮容器
  9. const buttonContainer = document.createElement('div');
  10. buttonContainer.innerHTML = `
  11. <div class="button-container">
  12. <button class="btn" data-action="action1">按钮1</button>
  13. <button class="btn" data-action="action2">按钮2</button>
  14. <button class="btn" data-action="action3">按钮3</button>
  15. <button class="btn" data-action="action4">按钮4</button>
  16. </div>
  17. `;
  18. // 将按钮容器插入到目标父元素中
  19. if (targetContainer) {
  20. targetContainer.appendChild(buttonContainer);
  21. // 或者使用 insertBefore 方法在特定位置插入
  22. // targetContainer.insertBefore(buttonContainer, targetContainer.firstChild);
  23. }
  24. buttonContainer.querySelectorAll('.btn').forEach(button => {
  25. button.addEventListener('click', function() {
  26. const action = this.getAttribute('data-action');
  27. switch (action) {
  28. case 'action1':
  29. console.log('按钮1被点击');
  30. // 执行按钮1的操作
  31. break;
  32. case 'action2':
  33. console.log('按钮2被点击');
  34. // 执行按钮2的操作
  35. break;
  36. case 'action3':
  37. console.log('按钮3被点击');
  38. // 执行按钮3的操作
  39. break;
  40. case 'action4':
  41. console.log('按钮4被点击');
  42. // 执行按钮4的操作
  43. break;
  44. default:
  45. break;
  46. }
  47. });
  48. });
  49. }
  50. }

在这个代码示例中,我们首先找到一个目标父元素(例如ID为 "targetContainer" 的 div),然后将创建的 buttonContainer 添加到这个目标容器中。这可以让你控制放置的位置,使其不再出现在页面底部。如果没有这样的 ID,可以根据你的需求调整选择器以找到合适的位置。


内容由零声教学AI助手提供,问题来源于学员提问

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://golang.0voice.com/?id=22051

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?