【问题标题】:How do I get the rank of the button我如何获得按钮的排名
【发布时间】:2020-08-12 15:07:14
【问题描述】:
 <script>
    function showRank(){
      // I don't know what to do now 
    }
 <script>

 <section>
    <div data-index='1'>
        <p>First</p>
        <button  onclick='showRank()'>First</button>
    </div>
    <div data-index="2">
            <p>Second</p>
        <button onclick='showRank()'>Second</button>
    </div>
    <div data-index="3">
            <p>Third</p>
        <button onclick='showRank()'>Third</button>
    </div>
</section>

当我单击任何一个按钮时,我想要 console.log() 按钮所在的 div。 例如:

输入-:Clicked the button 'First'

输出-:

<div data-index='1'>
     <p>First</p>
     <button  onclick='showRank()'>First</button>
  </div>

【问题讨论】:

    标签: javascript custom-data-attribute


    【解决方案1】:

    我最简单的方法是像这样将参数传递给你的函数

    <script>
        function showRank(varRank){
         console.log(varRank) 
        }
     <script>
    
     <section>
        <div data-index='1'>
            <p>First</p>
            <button  onclick='showRank("First")'>First</button>
        </div>
        <div data-index="2">
                <p>Second</p>
            <button onclick='showRank("Second")'>Second</button>
        </div>
        <div data-index="3">
                <p>Third</p>
            <button onclick='showRank("Third")'>Third</button>
        </div>
    </section>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多