简体   繁体   English

使用onClick获取javascript中单击按钮的ID

[英]using onClick to get the id of the clicked button in javascript

Iam swaping the values of the button and the .innerHTML of the buttons as well, but doing it in a in-efficient way . Iam交换按钮的值以及按钮的.innerHTML,但是这样做效率不高。 Like this : 像这样 :

function btnswap2()
{

    var soft=document.getElementById('1');
    var hard=document.getElementById('2');

    var temp;
    var temp1;
    var flag=0;

    if(soft.id==1)
        {
            temp1=soft.innerHTML;
            soft.innerHTML=hard.innerHTML;
            hard.innerHTML=temp1;

            temp=soft;
            soft=hard;
            hard=soft;

            showHARD();
        }
}

Now can i get the id of the button clicked in run-time, as you can see that the buttons get swapped every time i click the button. 现在我可以获取运行时单击的按钮的ID了,因为您可以看到每次单击按钮时按钮都会被交换。 Issue is i have to call some other function as here(in this case if it is hard then it should be hard else soft should be called) 问题是我必须在这里调用其他函数(在这种情况下,如果很难,则应该很困难,否则应调用柔软)

  showHARD();
  showSOFT();

How can i achieve this ??? 我该如何实现??? my butn id are as follows: BTN : ID 我的按钮ID如下:BTN:ID

 SOFT: 1
 HARD: 2
 TRAI: 3
 STAT:4 

Try adding a class to the static button: 尝试将类添加到静态按钮:

<button id="1" >button1</button>
<button id="2" >button2</button>
<button id="3" >button3</button>
<button id="4" class="static" >button4</button>

Then with jQuery: 然后用jQuery:

$(document).ready(function(){
 $("button").click(function(){

  if($(this).hasClass("static")){
   showSTAT();
  }else{
   btnswap2(this); 
   if($(this).attr("id")=="1")
    showSOFT();
   else if($(this).attr("id")=="2")
    showHARD();
   else
    showTRAI();
  }
 });
});


function btnswap2(btn){

var stat=$('button.static');

var temp;

temp=btn.html();
btn.html(stat.html());
stat.html(temp1);

stat.removeClass("static");
btn.addClass("static");
}

Don't call showSOFT() or showHARD() in the btnswap2() function! 不要在btnswap2()函数中调用showSOFT()或showHARD()!

In this way you will get always the static button and it will swap only if the button swapped is not static. 这样,您将始终获得静态按钮,并且仅当交换的按钮不是静态按钮时,它才会交换。

I guess what you would like to do is something like this: 我想您想做的是这样的:

HTML HTML

--------
static:
--------
<div id="1" >button A</div>

--------
live:
--------
<div id="2" onClick="btnswap2(this.id)">button B</div>
<div id="3" onClick="btnswap2(this.id)">button C</div>
<div id="4" onClick="btnswap2(this.id)">button D</div>

JavaScript JavaScript的

function btnswap2(id){
    var temp = document.getElementById("1").innerHTML; // static button
    var clicked_label = document.getElementById(id).innerHTML;
    document.getElementById("1").innerHTML = clicked_label;
    document.getElementById(id).innerHTML = temp;
}

Fiddle is here : http://jsfiddle.net/n5UDu/6/ 小提琴在这里: http : //jsfiddle.net/n5UDu/6/

Hope this helps. 希望这可以帮助。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM