【问题标题】:How do I pass button as a parameter to a function?如何将按钮作为参数传递给函数?
【发布时间】:2021-10-25 14:09:00
【问题描述】:

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

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

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

【问题讨论】:

  • function B(btn) { ... } 很好。你遇到错误了吗?错误是什么?

标签: javascript webforms


【解决方案1】:

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

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()

【讨论】:

    【解决方案2】:

    它应该可以正常工作。您看到那里有任何错误吗?

    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()

    【讨论】:

      【解决方案3】:

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

      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>

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-08-03
        • 2013-01-27
        • 2021-01-20
        • 1970-01-01
        • 2018-06-21
        • 2019-04-01
        • 2020-12-18
        • 2010-09-17
        相关资源
        最近更新 更多