Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/469.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 单击后如何禁用按钮_Javascript - Fatal编程技术网

Javascript 单击后如何禁用按钮

Javascript 单击后如何禁用按钮,javascript,Javascript,我正在使用以下脚本设置颜色: <script type="text/javascript"> if (localStorage.buttonColor) { document.getElementsByTagName('html')[0].className = localStorage.buttonColor } function getButtonColor(buttonName) { localStorage.button

我正在使用以下脚本设置颜色:

<script type="text/javascript">
    if (localStorage.buttonColor) {
        document.getElementsByTagName('html')[0].className = localStorage.buttonColor
    }
    function getButtonColor(buttonName) {
        localStorage.buttonColor = buttonName;
        document.getElementsByTagName('html')[0].className = buttonName
    }
</script>

if(localStorage.buttonColor){
document.getElementsByTagName('html')[0]。className=localStorage.buttonColor
}
函数getButtonColor(buttonName){
localStorage.buttonColor=buttonName;
document.getElementsByTagName('html')[0]。className=buttonName
}
这是我的HTML:

<form class="ng-pristine ng-valid">
   <button name="darkBlue" onclick="getButtonColor(this.name)">Blue</button>
   <button name="black" onclick="getButtonColor(this.name)">Black</button>
</form>

蓝色
黑色
如何使选择颜色时,选择该颜色的按钮 是否已禁用,因此无法再次选择?然后,当单击另一个按钮时,将启用其他按钮。我还需要将从localstorage中选择的按钮设置为disabled。对不起,我之前的问题中没有完全提到这一点

function getButtonColor(button) {
    button.disabled = "disabled"
    localStorage.buttonColor = button.name;
    document.getElementsByTagName('html')[0].className = button.name
}
只需发送此:

<button name="darkBlue" onclick="getButtonColor(this)">Blue</button>
<button name="black" onclick="getButtonColor(this)">Black</button>
蓝色
黑色


内联javascript是邪恶的

除了其他答案(只需发送处理程序中的按钮),您可以在最初从localStorage设置颜色时使用它(假设您的“表单”是“body”的第一个子项):

if(localStorage.buttonColor){
document.getElementsByTagName('html')[0]。className=localStorage.buttonColor
var buttons=document.body.getElementsByTagName('form')[0]。getElementsByTagName('button'))
对于(变量i=0;i

尽管您经常需要在代码中查找元素,但您可能需要考虑使用jQuery,因为GETelEntsByTAGNOTES选项可以得到详细说明。

您也可以使用:

function getButtonColor(button) {        
var elem=documentt.getElementById(button.id);
elem.removeAttribute("onclick");
}

首选方法是使用this并将其绑定到一个变量(通常是该变量)。 在这里,您将得到调用该函数的html对象


您还可以将
document.getElementsByTagName('html')[0]
document.documentElement
@gdoron切换-谢谢,但如果该按钮颜色存储在localstorage中并在页面加载时选中,我想这不会解决问题。此外,如果我单击按钮,解决方案将禁用“单击”,但如果单击其他按钮,则需要启用第一个按钮,否则它将不再工作。@Marilou,是的,当您更新问题时,答案显然不同步…@gdoron-你好。很抱歉,这不是一个真正的改变,只是一个澄清。我的意思是,用你的实现,它实际上只需要点击一次就可以工作,然后我的页面就不再工作了。我想代码中会很清楚,所以我没有详细说明。再次为更新感到抱歉。现在我真的不知道如何处理它。我应该再问一个问题吗?让我知道你的想法。谢谢您的解决方案不会禁用按钮。谢谢,但如果按钮颜色存储在localstorage中并在页面加载时选择,我认为这不会解决问题。此外,我还需要它,以便如果单击了另一个按钮,其余按钮需要全部启用。我们将此标记为正确,因为它是检查本地存储并将按钮状态更改为禁用,将其他按钮状态更改为启用的唯一解决方案。这还不是我们想要的,所以我的队友提出了另一个问题。谢谢你的帮助。
function getButtonColor(button) {        
var elem=documentt.getElementById(button.id);
elem.removeAttribute("onclick");
}
 function getButtonColor() {
        var that = this; 
        localStorage.buttonColor = that.Name;
        document.getElementsByTagName('html')[0].className = that.Name; 
        that.disabled = "disabled"; 
    }