使用JavaScript交换两个图像的位置

使用JavaScript交换两个图像的位置,javascript,image,swap,Javascript,Image,Swap,因此,我试图使它,如果你点击按钮,它将切换图像的位置。但是,它实际上并不切换位置,而是更改每个图像ID的src。当您单击按钮一次时,它会起作用,但之后图像不再切换。这是我的密码 function swapImages(){ var image1 = document.getElementById("image1") var image2 = document.getElementById("image2") if (image1.src = '/jmurphy9/111/

因此,我试图使它,如果你点击按钮,它将切换图像的位置。但是,它实际上并不切换位置,而是更改每个图像ID的src。当您单击按钮一次时,它会起作用,但之后图像不再切换。这是我的密码

function swapImages(){
    var image1 = document.getElementById("image1")
    var image2 = document.getElementById("image2")
    if (image1.src = '/jmurphy9/111/images/earthrise.jpg') {
        image1.src = '/jmurphy9/111/images/earth.jpg';
    } else {
        image1.src = '/jmurphy9/111/images/earthrise.jpg';
}
    if (image2.src = '/jmurphy9/111/images/earth.jpg') {
        image2.src = '/jmurphy9/111/images/earthrise.jpg';
}     else {
        image2.src = '/jmurphy9/111/images/earth.jpg';
}
}

function init(){
    var button1 = document.getElementById("btn1")
    button1.onclick = swapImages;
}

window.onload = init;

问题是
src
属性将具有图像的绝对路径,而不是您正在检查的相对路径

一种可能的解决方案是使用.indexOf(),如下所示

function swapImages() {
    var image1 = document.getElementById("image1")
    var image2 = document.getElementById("image2")
    if (image1.src.indexOf('/jmurphy9/111/images/earthrise.jpg')>-1) {
        image1.src = '/jmurphy9/111/images/earth.jpg';
    } else {
        image1.src = '/jmurphy9/111/images/earthrise.jpg';
    }
    if (image2.src.indexOf( '/jmurphy9/111/images/earth.jpg')>-1) {
        image2.src = '/jmurphy9/111/images/earthrise.jpg';
    } else {
        image2.src = '/jmurphy9/111/images/earth.jpg';
    }
}

或者你可以使用


但是既然你想交换,你可以这样做

function swapImages() {
    var image1 = document.getElementById("image1")
    var image2 = document.getElementById("image2")
    var src = image1.src;
    image1.src = image2.src;
    image2.src = src;
}
演示:



注意:在
if
条件中,使用赋值(
=
)运算符而不是比较运算符(
=
),因此,
if
中的
image1.src='/jmurphy9/111/images/earthrise.jpg'
应该是
image1.src='/jmurphy9/111/images/earthrise.jpg'
问题在于
src
属性将具有指向图像的绝对路径,而不是您正在检查的相对路径

一种可能的解决方案是使用.indexOf(),如下所示

function swapImages() {
    var image1 = document.getElementById("image1")
    var image2 = document.getElementById("image2")
    if (image1.src.indexOf('/jmurphy9/111/images/earthrise.jpg')>-1) {
        image1.src = '/jmurphy9/111/images/earth.jpg';
    } else {
        image1.src = '/jmurphy9/111/images/earthrise.jpg';
    }
    if (image2.src.indexOf( '/jmurphy9/111/images/earth.jpg')>-1) {
        image2.src = '/jmurphy9/111/images/earthrise.jpg';
    } else {
        image2.src = '/jmurphy9/111/images/earth.jpg';
    }
}

或者你可以使用


但是既然你想交换,你可以这样做

function swapImages() {
    var image1 = document.getElementById("image1")
    var image2 = document.getElementById("image2")
    var src = image1.src;
    image1.src = image2.src;
    image2.src = src;
}
演示:



注意:在
if
条件中,使用赋值(
=
)运算符而不是比较运算符(
=
),因此,
if
中的
image1.src='/jmurphy9/111/images/earthrise.jpg'
应该是
image1.src='/jmurphy9/111/images/earthrise.jpg'
问题在于
src
属性将具有指向图像的绝对路径,而不是您正在检查的相对路径

一种可能的解决方案是使用.indexOf(),如下所示

function swapImages() {
    var image1 = document.getElementById("image1")
    var image2 = document.getElementById("image2")
    if (image1.src.indexOf('/jmurphy9/111/images/earthrise.jpg')>-1) {
        image1.src = '/jmurphy9/111/images/earth.jpg';
    } else {
        image1.src = '/jmurphy9/111/images/earthrise.jpg';
    }
    if (image2.src.indexOf( '/jmurphy9/111/images/earth.jpg')>-1) {
        image2.src = '/jmurphy9/111/images/earthrise.jpg';
    } else {
        image2.src = '/jmurphy9/111/images/earth.jpg';
    }
}

或者你可以使用


但是既然你想交换,你可以这样做

function swapImages() {
    var image1 = document.getElementById("image1")
    var image2 = document.getElementById("image2")
    var src = image1.src;
    image1.src = image2.src;
    image2.src = src;
}
演示:



注意:在
if
条件中,使用赋值(
=
)运算符而不是比较运算符(
=
),因此,
if
中的
image1.src='/jmurphy9/111/images/earthrise.jpg'
应该是
image1.src='/jmurphy9/111/images/earthrise.jpg'
问题在于
src
属性将具有指向图像的绝对路径,而不是您正在检查的相对路径

一种可能的解决方案是使用.indexOf(),如下所示

function swapImages() {
    var image1 = document.getElementById("image1")
    var image2 = document.getElementById("image2")
    if (image1.src.indexOf('/jmurphy9/111/images/earthrise.jpg')>-1) {
        image1.src = '/jmurphy9/111/images/earth.jpg';
    } else {
        image1.src = '/jmurphy9/111/images/earthrise.jpg';
    }
    if (image2.src.indexOf( '/jmurphy9/111/images/earth.jpg')>-1) {
        image2.src = '/jmurphy9/111/images/earthrise.jpg';
    } else {
        image2.src = '/jmurphy9/111/images/earth.jpg';
    }
}

或者你可以使用


但是既然你想交换,你可以这样做

function swapImages() {
    var image1 = document.getElementById("image1")
    var image2 = document.getElementById("image2")
    var src = image1.src;
    image1.src = image2.src;
    image2.src = src;
}
演示:



注意:在
if
条件中,使用赋值(
=
)运算符而不是比较运算符(
=
),因此,
if
中的
image1.src='/jmurphy9/111/images/earthrise.jpg'
应该是
image1.src='/jmurphy9/111/images/earthrise.jpg'

看起来您的等式运算符在if上缺少了一个额外的“=”,例如if(image1.src=='/jmurphy9/111/images/earthrise.jpg'))因此,当它试图计算表达式时,它从未进入,所以它第一次改变,因为它进入了else,从那里开始,它一直进入else,所以什么也没有发生

function swapImages(){
    var image1 = document.getElementById("image1")
    var image2 = document.getElementById("image2")
    if (image1.src == '/jmurphy9/111/images/earthrise.jpg') {
        image1.src = '/jmurphy9/111/images/earth.jpg';
    } else {
        image1.src = '/jmurphy9/111/images/earthrise.jpg';
}
    if (image2.src == '/jmurphy9/111/images/earth.jpg') {
        image2.src = '/jmurphy9/111/images/earthrise.jpg';
}     else {
        image2.src = '/jmurphy9/111/images/earth.jpg';
}
}    

function init(){
    var button1 = document.getElementById("btn1")
    button1.onclick = swapImages;
}

window.onload = init;

看起来您的相等运算符在if上缺少了一个额外的“=”,例如if(image1.src='/jmurphy9/111/images/earthrise.jpg'),因此当它尝试计算表达式时,它永远不会进入,所以它会在第一次更改,因为它进入了else,然后继续进入else,所以什么也不会发生

function swapImages(){
    var image1 = document.getElementById("image1")
    var image2 = document.getElementById("image2")
    if (image1.src == '/jmurphy9/111/images/earthrise.jpg') {
        image1.src = '/jmurphy9/111/images/earth.jpg';
    } else {
        image1.src = '/jmurphy9/111/images/earthrise.jpg';
}
    if (image2.src == '/jmurphy9/111/images/earth.jpg') {
        image2.src = '/jmurphy9/111/images/earthrise.jpg';
}     else {
        image2.src = '/jmurphy9/111/images/earth.jpg';
}
}    

function init(){
    var button1 = document.getElementById("btn1")
    button1.onclick = swapImages;
}

window.onload = init;

看起来您的相等运算符在if上缺少了一个额外的“=”,例如if(image1.src='/jmurphy9/111/images/earthrise.jpg'),因此当它尝试计算表达式时,它永远不会进入,所以它会在第一次更改,因为它进入了else,然后继续进入else,所以什么也不会发生

function swapImages(){
    var image1 = document.getElementById("image1")
    var image2 = document.getElementById("image2")
    if (image1.src == '/jmurphy9/111/images/earthrise.jpg') {
        image1.src = '/jmurphy9/111/images/earth.jpg';
    } else {
        image1.src = '/jmurphy9/111/images/earthrise.jpg';
}
    if (image2.src == '/jmurphy9/111/images/earth.jpg') {
        image2.src = '/jmurphy9/111/images/earthrise.jpg';
}     else {
        image2.src = '/jmurphy9/111/images/earth.jpg';
}
}    

function init(){
    var button1 = document.getElementById("btn1")
    button1.onclick = swapImages;
}

window.onload = init;

看起来您的相等运算符在if上缺少了一个额外的“=”,例如if(image1.src='/jmurphy9/111/images/earthrise.jpg'),因此当它尝试计算表达式时,它永远不会进入,所以它会在第一次更改,因为它进入了else,然后继续进入else,所以什么也不会发生

function swapImages(){
    var image1 = document.getElementById("image1")
    var image2 = document.getElementById("image2")
    if (image1.src == '/jmurphy9/111/images/earthrise.jpg') {
        image1.src = '/jmurphy9/111/images/earth.jpg';
    } else {
        image1.src = '/jmurphy9/111/images/earthrise.jpg';
}
    if (image2.src == '/jmurphy9/111/images/earth.jpg') {
        image2.src = '/jmurphy9/111/images/earthrise.jpg';
}     else {
        image2.src = '/jmurphy9/111/images/earth.jpg';
}
}    

function init(){
    var button1 = document.getElementById("btn1")
    button1.onclick = swapImages;
}

window.onload = init;

接受的答案与您的答案一样,交换图像的src属性

另一种方法是实际交换img节点本身,如果图像有各自的重要性,例如需要特殊的事件处理或css注意事项(取决于实际图像),这可能更可取

function swapNodes(a, b){
    var p= b.parentNode, sib= b.nextSibling;
    if(sib=== a) sib= sib.nextSibling;
    a.parentNode.replaceChild(b, a);
    return sib? p.insertBefore(a, sib): p.appendChild(a);
}
请注意,始终可以交换img节点或任意两个类似节点,但有些节点无法交换,因为其中一个节点的容器节点将不接受另一个节点作为子节点


如果它们是有效的html,您可以交换它们。

接受的答案与您的答案一样,交换图像的src属性

另一种方法是实际交换img节点本身,如果图像有各自的重要性,例如需要特殊的事件处理或css注意事项(取决于实际图像),这可能更可取

function swapNodes(a, b){
    var p= b.parentNode, sib= b.nextSibling;
    if(sib=== a) sib= sib.nextSibling;
    a.parentNode.replaceChild(b, a);
    return sib? p.insertBefore(a, sib): p.appendChild(a);
}
请注意,始终可以交换img节点或任意两个类似节点,但有些节点无法交换,因为其中一个节点的容器节点将不接受另一个节点作为子节点


如果它们是有效的html,您可以交换它们。

接受的答案与您的答案一样,交换图像的src属性

另一种方法是实际交换img节点本身,如果图像有各自的重要性,例如需要特殊的事件处理或css注意事项(取决于实际图像),这可能更可取

function swapNodes(a, b){
    var p= b.parentNode, sib= b.nextSibling;
    if(sib=== a) sib= sib.nextSibling;
    a.parentNode.replaceChild(b, a);
    return sib? p.insertBefore(a, sib): p.appendChild(a);
}
请注意,始终可以交换img节点或任意两个类似节点,但有些节点无法交换,因为其中一个节点的容器节点将不接受另一个节点作为子节点


如果它们是有效的html,您可以交换它们。

接受的答案与您的答案一样,交换图像的src属性

另一种方法是实际交换img节点本身,如果图像有各自的重要性,例如需要特殊的事件处理或css考虑,这可能更可取