带有更改网址的if语句

时间:2017-04-02 18:11:55

标签: javascript html if-statement

这个功能有什么问题?第一个if语句确实有效。但是else语句不起作用。

function func() {
    if (document.getElementById("pic2").src = "pause.png") {
        document.getElementById("pic2").src = "play.png";
    } else {
        document.getElementById("pic2").src == "pause.png";
    }
}

HTML:

<div id="all" onclick="func()" onmouseover="func3()">
            <div id="background" onclick="func()" onmouseout="func2()"></div>
            <div id="pic">
                <img id="pic2" src="pause.png">
            </div>
            <video>
                <source src="test.mp4" type="video/mp4">
            </video>
        </div>

3 个答案:

答案 0 :(得分:1)

很少有东西不能让你切换你的img src。

  1. 条件检查不当。 if (document.getElementById("pic2").src = "pause.png")必须为if (document.getElementById("pic2").src == "pause.png")
  2. document.getElementById("pic2").src会为您提供图片的完整路径。最好使用img_url.substr(img_url.lastIndexOf("/")+1);
  3. 将其修剪为图像名称(带扩展名)

    &#13;
    &#13;
    function func() {
        var img_url = document.getElementById("pic2").src;
        var img = img_url.substr(img_url.lastIndexOf("/")+1);
        if (img  == "pause.png") {
            document.getElementById("pic2").src = "play.png";
        } else {
            document.getElementById("pic2").src = "pause.png";
        }
    }
    
    function func2(){
     // your func2
    }
    
    function func3(){
     // your func3
    }
    &#13;
    <div id="all" onclick="func()" onmouseover="func3()">
                <div id="background" onclick="func()" onmouseout="func2()"></div>
                <div id="pic">
                    <img id="pic2" src="pause.png">
                </div>
                <video>
                    <source src="test.mp4" type="video/mp4">
                </video>
            </div>
    &#13;
    &#13;
    &#13;

答案 1 :(得分:0)

单个等号标志用于分配值。你必须添加两个等于比较两个变量或值。
像这样: -

function func() {
    if (document.getElementById("pic2").src == "pause.png") {
        document.getElementById("pic2").src = "play.png";
    } else {
        document.getElementById("pic2").src = "pause.png";
    }
}

答案 2 :(得分:0)

试试这个:

s.close()
# the socket is closed, you can't use it anymore!

# get another one:
s2 = socket.socket()
s2.connect(('127.0.0.1',5000))
s2.close()
# now s2 is closed, you can't use it anymore!

祝你好运