简体   繁体   English

我该如何解决从函数内部调用函数的元素?

[英]How can I address the element that called a function from within the function?

I am trying this HTML code: 我正在尝试以下HTML代码:

<button name="darkBlue" onclick="setThemeColor(this.name)">Blue</button>
<button name="black" onclick="setThemeColor(this.name)">Black</button>

and script: 和脚本:

function setThemeColor(buttonName) {
    localStorage.themeColor = buttonName;
    document.getElementsByTagName('html')[0].className = buttonName
    var themeButtons = document.querySelectorAll(".theme");
    for (var button in themeButtons) {
        themeButtons[button].disabled = false;
    }
    // this.disabled = false;
    // element.setAttribute("disabled", "disabled");
}

I am having a problem here setting the disabled state of the button that called the function. 我在设置调用该函数的按钮的禁用状态时遇到问题。 Can someone tell me how i can do this. 有人可以告诉我我该怎么做。 I have tried two things but neither seem to work. 我尝试了两件事,但似乎都没有用。

Pass in a reference to your button instead of just the name: 传递对按钮的引用,而不只是名称:

HTML 的HTML

<button name="darkBlue" onclick="setThemeColor(this)">Blue</button>
<button name="black" onclick="setThemeColor(this)">Black</button>

JS JS

function setThemeColor(button) {
    localStorage.themeColor = button.name;
    document.getElementsByTagName('html')[0].className = button.name;
    var themeButtons = document.querySelectorAll(".theme");
    for (var button in themeButtons) {
        themeButtons[button].setAttribute("disabled", "disabled");
    }
    button.setAttribute("disabled", "disabled");
}
document.getElementById("buttonid1").disabled=false;

暂无
暂无

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

相关问题 如何从调用的函数中引用包含事件的元素? - How can I reference the element that contains the event from the function called? 在Javascript中,如何从该函数中调用但在其他位置定义的函数中引用函数范围的变量? - In Javascript, how can I reference a function-scoped variable from a function called within that function but defined elsewhere? 我可以声明一个可以在任何对象内调用的函数吗? - Can i declare a function that could be called from within any object? 如何获取调用 function 的元素的 innerText? - How can I get the innerText of the element that called the function? 如何在jQuery的ajax函数中从类访问元素ID? - How can I access the element ID from a class, within an ajax function in jQuery? 如何在其OnBegin函数中引用单击的ActionLink`a`元素? - How can I reference a clicked ActionLink `a` element from within it's OnBegin function? 如何在事件处理程序中包含一个函数(及其参数),而无需在页面加载时调用该函数? - How can I include a function (and its parameter) within an event handler, without the function being called on page load? 如何在处理程序调用的函数中引用clicked元素 - How to reference the clicked element within a function that is called by the handler 一旦在 JavaScript 中满足条件,我将如何使事件处理程序函数中的被调用函数无法运行? - How would I void a called function within an event Handler function from running once a condition is met in JavaScript? 如何断言一个函数是从另一个函数中调用的? - How to assert a function is called from within another function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM