Javascript 如何更改图像链接onclick
我试图改变点击链接的图像Javascript 如何更改图像链接onclick,javascript,html,Javascript,Html,我试图改变点击链接的图像 <div class="mobilestoreimg"> <a href="/store-locator"><img src="../includes/mobile-location.png"/></a> </div> 太 有什么想法吗?您可以在 <div class="mobilestoreimg"> <a href="/store-locator"><img
<div class="mobilestoreimg">
<a href="/store-locator"><img src="../includes/mobile-location.png"/></a>
</div>
太
有什么想法吗?您可以在
<div class="mobilestoreimg">
<a href="/store-locator"><img id="imgChange" src="../includes/mobile-location.png"/></a>
</div>
使用jQuery的替代版本:
$(function() {
$('a').click(function(event) {
event.preventDefault();
$('img').attr('src',"newValue");
})
})
我做了一个jsfiddle,但该站点不喜欢中断事件,不管怎样,如果您需要,它就在这里,但href:上没有链接,试试这个。这对你真的很有帮助
如果您使用的是jQuery
$(function() {
$('.mobilestoreimg>a').click(function(event) {
event.preventDefault();
$('img').attr('src',"newValue");
})
})
否则
document.querySelector('.mobilestoreimg>a').addEventListener('click, function() { this.querySelector('img').src = 'newValue'; }, false);
加载jQuery、加载jQuery UI、加载angularjs、加载requirejs(只是为了确保)。然后编写document.querySelector('mobilestoreimg').addEventListener('click,function(){this.querySelector('img').src='mobile location blk.png';},false)
然后删除jQuery,删除jqueryui,删除angularjs,并删除requirejs。然后在googleA上搜索addEventListener javascript
,点击链接会将您转发到一个新位置。为什么要更改该链接的图像?为了显示您已单击该链接且页面正在加载,一旦单击该链接,图像将保持不变,我真的不明白为什么人们坚持使用jquery来处理如此简单的事情。如果没有它就可以安全方便地携带,那么这将是一笔很大的开销。@M0n5terBunny这正是你所要求的。如果你想让src
切换,你需要问这个问题。你是否有一个suggestionwell切换以便你点击链接,它会改变图像以显示你点击了图像并加载了页面
$(function() {
$('a').click(function(event) {
event.preventDefault();
$('img').attr('src',"newValue");
})
})
$(function() {
$('.mobilestoreimg>a').click(function(event) {
event.preventDefault();
$('img').attr('src',"newValue");
})
})
document.querySelector('.mobilestoreimg>a').addEventListener('click, function() { this.querySelector('img').src = 'newValue'; }, false);