我想切換點擊特定圖片元素的src。但是,只爲了那個形象。換句話說,只切換一個點擊。如果再次點擊,然後改回來。我怎樣才能用jQuery或JavaScript來做到這一點。該代碼可以在這裏看到:https://codepen.io/centem/pen/NgKEmG更改特定點擊元素的圖片
<!--
When image clicked change image to:
https://www.centerpointe.com/v2/wp-content/uploads/2014/01/red-x.png
-->
<ul class="list-unstyled">
<li>Username<span class="pull-right">
<img src="https://upload.wikimedia.org/wikipedia/commons/8/87/Google_Chrome_icon_%282011%29.png"></span><span class="pull-right">
<img src="http://pngimg.com/uploads/firefox/firefox_PNG16.png"></span><span class="pull-right">
<img src="https://ma.ttias.be/wp-content/uploads/2015/07/internet-explorer-logo.png"></span></li>
<li>Username<span class="pull-right">
<img src="https://upload.wikimedia.org/wikipedia/commons/8/87/Google_Chrome_icon_%282011%29.png"></span><span class="pull-right">
<img src="http://pngimg.com/uploads/firefox/firefox_PNG16.png"></span><span class="pull-right">
<img src="https://ma.ttias.be/wp-content/uploads/2015/07/internet-explorer-logo.png"></span></li></li>
<li>Username<span class="pull-right">
<img src="https://upload.wikimedia.org/wikipedia/commons/8/87/Google_Chrome_icon_%282011%29.png"></span><span class="pull-right">
<img src="http://pngimg.com/uploads/firefox/firefox_PNG16.png"></span><span class="pull-right">
<img src="https://ma.ttias.be/wp-content/uploads/2015/07/internet-explorer-logo.png"></span></li></li>
</ul>
這裏是我的jQuery:
function handler(event) {
var target = $(event.target);
if (target.is("img")) {
target.src().toggle(
"https://www.centerpointe.com/v2/wp-content/uploads/2014/01/red-x.png");
}
}
謝謝。
Pos sible重複[以編程方式更改img標籤的src](https://stackoverflow.com/questions/11722400/programmatically-change-the-src-of-an-img-tag) – Dez
您的codepen使$('。 chrome')。attr('src',...),改變所有$('。chrome')。更改爲$('this).attr('src',...) –