首页 > 解决方案 > JavaScript 函数中的函数

问题描述

我在按钮上有提交功能。单击“提交”后会弹出一个对话框并要求确认,单击“确定”后会提交表单。

现在我想添加另一个功能,点击“确定”后,表单应该被提交并且页面的背景颜色也应该改变。我写的代码如下。但现在连表格都没有提交。需要帮助!

function myfunction(e)
    {
        if(!confirm('Are you sure to proceed with this role?')) {
            function changecolor(){
                backgroundcolor: #0065bd;
            }
            changecolor();
            e.preventDefault();
        }
        else {
            return false;
        }
    }

标签: javascripthtml

解决方案


changecolor您可以在全局上添加该功能。无需在另一个函数中添加。并通过参数将数据从第一个函数传递到第二个函数

更新

通过更改颜色值classList.add

function myfunction(e) {
  if (!confirm('Are you sure to proceed with this role?')) {
    changecolor(e); //pass anything
    e.preventDefault();
  } else {
    return false;
  }
}

function changecolor(e) { //receive
  e.target.classList.add('custom_back')
}

css: 更新了你的代码style.css

.custom_back{
   backgroundcolor: #0065bd !important;
}

推荐阅读