Cambiar programáticamente el src de una etiqueta img
¿Cómo puedo cambiar el atributo src
de una etiqueta img
usando javascript?
<img src="../template/edit.png" name=edit-save/>
Al principio tengo un src predeterminado que es "../ template / edit.png" y quería cambiarlo con"../ template / save.png " onclick.
ACTUALIZADO: aquí está mi html onclick:
<a href='#' onclick='edit()'><img src="../template/edit.png" id="edit-save"/></a>
Y mi JS
function edit()
{
var inputs = document.myform;
for(var i = 0; i < inputs.length; i++) {
inputs[i].disabled = false;
}
}
He intentado insertar esto dentro de edit(), funciona pero necesito hacer clic en la imagen dos veces
var edit_save = document.getElementById("edit-save");
edit_save.onclick = function(){
this.src = "../template/save.png";
}
9 answers
Dale a tu etiqueta img un id, entonces puedes
document.getElementById("imageid").src="../template/save.png";
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-01-24 07:15:17
Puede usar el método jquery y javascript: si tienes dos imágenes por ejemplo:
<img class="image1" src="image1.jpg" alt="image">
<img class="image2" src="image2.jpg" alt="image">
1) Método Jquery - >
$(".image2").attr("src","image1.jpg");
2) Método Javascript - >
var image = document.getElementsByClassName("image2");
image.src = "image1.jpg"
Para este tipo de problema, jquery es el más sencillo de usar.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-03-25 15:56:12
Si usa la biblioteca jQuery, use esta instrucción:
$("#imageID").attr('src', 'srcImage.jpg');
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-01-22 09:01:57
Está bien ahora
function edit()
{
var inputs = document.myform;
for(var i = 0; i < inputs.length; i++) {
inputs[i].disabled = false;
}
var edit_save = document.getElementById("edit-save");
edit_save.src = "../template/save.png";
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-07-30 14:33:07
<img src="../template/edit.png" name="edit-save" onclick="this.src = '../template/save.png'" />
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-07-30 13:20:50
En este caso, como desea cambiar el src
del primer valor de su elemento, no tiene necesidad de construir una función. Puede cambiar esto directamente en el elemento:
<a href='#' onclick='this.firstChild.src="../template/save.png"')'>
<img src="../template/edit.png" id="edit-save"/>
</a>
Usted tiene varias maneras de hacer esto. También puede crear una función para automatizar el proceso:
function changeSrc(p, t) { /* where p: Parent, t: ToSource */
p.firstChild.src = t
}
Entonces puedes:
<a href='#' onclick='changeSrc(this, "../template/save.png");'>
<img src="../template/edit.png" id="edit-save"/>
</a>
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-10-03 17:24:20
Con el fragmento que proporcionó (y sin hacer suposiciones sobre los padres del elemento) podría obtener una referencia a la imagen con
document.querySelector('img[name="edit-save"]');
Y cambiar el src con
document.querySelector('img[name="edit-save"]').src = "..."
Para que pueda lograr el efecto deseado con
var img = document.querySelector('img[name="edit-save"]');
img.onclick = function() {
this.src = "..." // this is the reference to the image itself
};
De lo contrario, como otros sugieren, si usted está en control del código, es mejor asignar un id
a la imagen a obtener una referencia con getElementById
(ya que es el método más rápido para recuperar un elemento)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-07-30 13:26:39
Dale un id a tu imagen. Entonces puedes hacer esto en tu javascript.
document.getElementById("blaah").src="blaah";
Puede utilizar el ".___" método para cambiar el valor de cualquier atributo de cualquier elemento.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-10-03 17:15:55
Tal vez porque tienes una etiqueta como un padre de la etiqueta. Por eso tienes que hacer clic dos veces en las imágenes.
Para mí la solución es esta: http://www.w3schools.com/js/tryit.asp?filename=tryjs_intro_lightbulb
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-01-19 09:06:22