简体   繁体   English

如何在点击时更改按钮图像?

[英]How to change button image on click?

I am using images and hrefs to create buttons. 我正在使用图像和href创建按钮。 Code look like this: 代码如下:

<a href="http://www.zive.sk"><img src="res/images/zive.png" alt="Zive.sk" id="ziveCircle"></a>

Is there any way to change image src if user press button? 如果用户按下按钮,是否有任何方法可以更改图像src? Maybe some event handler which will determine that user has pressed image? 也许某些事件处理程序将确定用户已按下图像? It should act as normal button so when user press it and keep pressed it should change image src and when he releases the button then the href should be loaded (normal button behaviour). 它应该充当普通按钮,因此,当用户按下并保持按下状态时,应该会更改图片的src值;当他释放按钮时,应加载href(正常按钮的行为)。

PS: I cannot use css for this. PS:我不能为此使用CSS。

This will change the button's image to your StackOverflow gravatar icon (change the URL accordingly): 这会将按钮的图像更改为您的StackOverflow头像图标(相应地更改URL):

HTML : HTML

<a id="zive-anchor" href="javascript:void(0);">
    <img id="zive-img" src="res/images/zive.png" alt="Zive.sk" id="ziveCircle">
</a>​

Javascript : Javascript

document.getElementById('zive-anchor').onclick=function () {
    document.getElementById('zive-img').src = 
    "http://www.gravatar.com/avatar/ca2a5b453d7e221f50929cbf6d688e59?s=32&d=identicon&r=PG";
};​

JSFiddle : JSFiddle

http://jsfiddle.net/CsCSC/ http://jsfiddle.net/CsCSC/

I have found a solution, using some of Hope I Helped script. 我已经找到了解决方案,使用了一些希望得到帮助的脚本。 Here is the code: 这是代码:

function buttons(){
document.getElementById('zive-anchor').onmousedown=function () {
    document.getElementById('ziveCircle').src = 
    "res/images/zivePressed.png";
};
document.getElementById('zive-anchor').onmouseout=function () {
    document.getElementById('ziveCircle').src = 
    "res/images/zive.png";
};
};

and here is HTML: 这是HTML:

<div data-role="page" id="homepage">
        <div id="selectCircle">
            <a href="feeds.html#page1" rel="external" id="zive-anchor"><img src="res/images/zive.png" alt="Zive.sk" id="ziveCircle"></a>
function play()
{



var player= document.getElementById("audio");


if(player.paused)

player.play();


else
player.pause(); 

change();

}

function change()
{

var imag = document.getElementById("play")

if(imag.src.match('Play'))
imag.src="pause.jpg";

else
imag.src="Play.jpg";



}

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

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