繁体   English   中英

如何使用JavaScript将按钮的onclick更改为另一个功能?

[英]How to change the onclick of a button to another function using JavaScript?

这是我要问的第一个问题。 是否可以将onclick="change()"为另一个函数,例如onclick="change1()"

我的代码是这样的:

<a href="#" onclick="changeSrc()" id="a1"><img src="images/transparent-arrow-hi.png" style="position:absolute; top:525px; left:1000px; height:50px; width:50px;" ></a>
<img src="images/keyboard.png" id="pic1">
<img src="images/mouse1.png" id="pic2">
<img src="images/PCstand.png" id="pic3">
<img src="images/monitor.png" id="pic4">

我的Javascript如下:

<script>
function changeSrc() {
document.getElementById('pic1').src="images/robber1.jpg";
document.getElementById('pic2').src="images/guarding entrance 1.jpg";
document.getElementById('pic3').src="images/Lock.ico";
document.getElementById('pic4').src="images/Number pads.jpg";
}
</script>

到目前为止,这在更改img src时有效,但是我想多次单击同一按钮将img src更改为其他按钮。

因此,它就像这个按钮一样,当onclick时,可以调用第一个函数来更改img src,同时准备onclick来调用第二个函数。

那有可能吗?

如果需要,我可以添加更多代码。

您可以拥有计数器,每次调用您的函数都会增加计数器的数量并选择所需的操作。也可以在changeSrc函数中使用switch

<script>
var counter = 0;
function changeSrc() {
 if(counter==0){
    document.getElementById('pic1').src="images/robber1.jpg";
    document.getElementById('pic2').src="images/guarding entrance 1.jpg";
    document.getElementById('pic3').src="images/Lock.ico";
    document.getElementById('pic4').src="images/Number pads.jpg";
}
 if(counter==1){
    document.getElementById('pic1').src="images/robber2.jpg";
    document.getElementById('pic2').src="images/guarding entrance 2.jpg";
    document.getElementById('pic3').src="images/Lock2.ico";
    document.getElementById('pic4').src="images/Number pads2.jpg";
}
    counter++;
}
</script>

如果您的图像具有数字之类的图案,则可以执行此操作

<script>
var counter = 0;
function changeSrc() {
    document.getElementById('pic1').src="images/robber"+counter+".jpg";
    document.getElementById('pic2').src="images/guarding entrance "+counter+".jpg";;
    document.getElementById('pic3').src="images/Lock "+counter+".ico";;
    document.getElementById('pic4').src="images/Number pads"+counter+".jpg";;    
 counter++;
}
</script>

您可以使用此代码段动态关联和更改元素的onclick函数。

 function changeSrc(_this){ //associate function change1 inside changeSrc $(_this).attr('onclick','change1(this)'); } function change1(_this){ //associate function change2 inside change1 $(_this).attr('onclick','change2(this)'); console.log('Iniside Change1()') } function change2(_this){ //associate function change3 inside change2 $(_this).attr('onclick','change3(this)'); console.log('Iniside Change2()') } function change3(_this){ console.log('Iniside Change3()') } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <a href="#" onclick="changeSrc(this)" id="a1"><img src="images/transparent-arrow-hi.png" style="position:absolute; top:525px; left:1000px; height:50px; width:50px;" >click</a> 

暂无
暂无

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

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