如何将按钮作为参数传递给函数?

How do I pass button as a parameter to a function?

我有一个调用另一个函数的函数。我的目标是将按钮作为参数传递给另一个函数。

function A(){
    var btn = $find("<%=btnX.ClientID %>");
    B(btn);
}

function B(*btn parameter here*){
//I need button here
}

你甚至没有遇到问题,因为它应该已经可以工作了...

function A(){
    // var btn = $find("<%=btnX.ClientID %>");
    btn = "test" // remove this "test" line
    B(btn);
}

function B(button){
//I need button here
console.log(button)
}

A()

它应该可以正常工作。你看到那里有什么错误吗?

function A(){
    var btn = $find("<%=btnX.ClientID %>");
    B(btn);
}

function B(button){
  // Do with that button whatever you want here. E.g.
  button.style.color = 'cyan';
}

A()

用任何选择器试试这个。它应该可以正常工作

function A(){
    var btn = "#btn";
    B(btn);
}

function B(button){
  // Do with that button whatever you want here. E.g.
  var btn = document.querySelector(button);

  btn.style.backgroundColor = 'red';
  btn.style.color = 'white';
}

A();

function A(){
    var btn = "#btn1";
    B(btn);
}

function B(button){
  // Do with that button whatever you want here. E.g.
  var btn = document.querySelector(button);
  
  console.log(button);
  
  btn.style.backgroundColor = 'red';
  btn.style.color = 'white';
}

A();
<button id="btn0">click button</button>
<button id="btn1">click button</button>