ZBLOG

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

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

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

for (const item of jsonRes) {
    if (item.code === 'customerInfo') {
        // 客户信息展示
    }
    if (item.code === 'loan') { // 合同展示
        
        // 找到目标父元素(假设它的ID是 "targetContainer")
        const targetContainer = document.getElementById('targetContainer');
        
        // 创建按钮容器
        const buttonContainer = document.createElement('div');
        buttonContainer.innerHTML = `
            <div class="button-container">
                <button class="btn" data-action="action1">按钮1</button>
                <button class="btn" data-action="action2">按钮2</button>
                <button class="btn" data-action="action3">按钮3</button>
                <button class="btn" data-action="action4">按钮4</button>
            </div>
        `;
        
        // 将按钮容器插入到目标父元素中
        if (targetContainer) {
            targetContainer.appendChild(buttonContainer);
            // 或者使用 insertBefore 方法在特定位置插入
            // targetContainer.insertBefore(buttonContainer, targetContainer.firstChild);
        }

        buttonContainer.querySelectorAll('.btn').forEach(button => {
            button.addEventListener('click', function() {
                const action = this.getAttribute('data-action');
                switch (action) {
                    case 'action1':
                        console.log('按钮1被点击');
                        // 执行按钮1的操作
                        break;
                    case 'action2':
                        console.log('按钮2被点击');
                        // 执行按钮2的操作
                        break;
                    case 'action3':
                        console.log('按钮3被点击');
                        // 执行按钮3的操作
                        break;
                    case 'action4':
                        console.log('按钮4被点击');
                        // 执行按钮4的操作
                        break;
                    default:
                        break;
                }
            });
        });
    }
}

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


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

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

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

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

联系我们

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

微信号:3007537140

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

关注我们
x

注册

已经有帐号?