简体   繁体   English

HTML img onclick javascript 功能不起作用

[英]HTML img onclick javascript function not working

Here's the html code.这是html代码。

<script type="text/javascript">
function func() {

    if (document.getElementById("heart").src == "heart_empty.png") 
    {
        document.getElementById("heart").src = "heart_filled.png";
    }
    else if(document.getElementById("heart").src == "heart_filled.png")
    {
        document.getElementById("heart").src = "heart_empty.png";
    }
}
</script>
<img id="heart" src='heart_empty.png' onclick="func()" />

Javascript function func() is not working. Javascript 函数 func() 不起作用。

Your second else if has a single = .你的第二个else if有一个= It should be a comparison, so it should use == like the first.它应该是一个比较,所以它应该像第一个一样使用==

Always try to use the strict comparison operator === to ensure that you're dealing with the same types.始终尝试使用严格比较运算符===以确保您处理的是相同的类型。 If the types are different, you should convert it before comparison.如果类型不同,您应该在比较之前将其转换。

function func() {
    if (document.getElementById("heart").src == "heart_empty.png")  {
        document.getElementById("heart").src = "heart_filled.png";
    } else if(document.getElementById("heart").src == "heart_filled.png") {
        document.getElementById("heart").src = "heart_empty.png";
    }
}

In addition, since it looks like there is only ever two states, you don't have to have a second else if .此外,由于看起来只有两个状态,因此您不必有第二个else if Just skip to the else .直接跳到else

function func() {
    var heart = document.getElementById("heart");

    if (heart.src == "heart_empty.png") {
        heart.src = "heart_filled.png";
    } else {
        heart.src = "heart_empty.png";
    }
}

Can you use background image instead?你可以用背景图片代替吗? So have a div and depending on the class you have different image as the background.所以有一个 div 并且根据课程你有不同的图像作为背景。

HTML HTML

<div class="heartImg filled"></div>

CSS CSS

.filled {
    background-image: url(filled.jpg);
}
.empty {
    background-image: url(empty.jpg);
}

Jquery查询

$('.heartImg').click(function(){
    $(this).toggleClass('filled empty');
}

Hello你好

Try to use the operator "?"尝试使用运算符“?” for this instance对于这个例子

Your current code looks like:您当前的代码如下所示:

function func() {
  if (document.getElementById("heart").src == "heart_empty.png") 
   {
  document.getElementById("heart").src = "heart_filled.png";
  }
  else if(document.getElementById("heart").src == "heart_filled.png")
  {
      document.getElementById("heart").src = "heart_empty.png";
  }
}

Instead, use the "?"相反,使用“?” operator to compact your function into one line:运算符将您的函数压缩为一行:

Also, instead of getting the element each time, add a script at the bottom of the page that looks like this:此外,不是每次都获取元素,而是在页面底部添加一个如下所示的脚本:

var src = document.getElementById("heart").src;

That will not only decrease lag but also will increase readability of your code这不仅会减少延迟,还会增加代码的可读性

The new script will use the fact that the "?"新脚本将使用“?”这一事实。 operator can combine an "if" statement into a one line declaration.运算符可以将“if”语句组合成一行声明。 To recap, the operator does this:回顾一下,操作员这样做:

variable = condition ? /*true*/ : /*false*/

Put your (true) code in the "true" bit and same with the false bit If the condition is true, then it will set the "variable" to the code written in the "true" bit, otherwise, it will set it to the code written in the "false" bit.将您的(真)代码放在“真”位,与假位相同如果条件为真,则将“变量”设置为“真”位中写入的代码,否则将其设置为写在“假”位的代码。

So now with this newfound knowledge, we can update our code!!!所以现在有了这些新发现的知识,我们可以更新我们的代码了!!!

NEW CODE:新代码:

function func() {
  document.getElementById("heart").src =
  document.getElementById("heart").src === "heart_empty.png" ?
  "heart_filled.png" : "heart_empty.png";
}

This can be further compacted by using the notation stated above where "src" variable = document.getElementById("heart").src, now it looks like:这可以通过使用上述符号进一步压缩,其中 "src" variable = document.getElementById("heart").src,现在它看起来像:

function func() {
  src = src === "heat_empty.png" ? "heart_filled.png" : "heart_empty.png";
}

Much Better!好多了! Now your code is neat, compact and flawless!现在您的代码整洁、紧凑且完美无瑕!

The second of your else if statements has one = rather than two.你的第二个 else if 语句有一个 = 而不是两个。 It needs to be a comparison, so use ==.需要比较,所以用==。

function func() {
    if (document.getElementById("heart").src == "heart_empty.png")  {
    document.getElementById("heart").src = "heart_filled.png";
    } else if(document.getElementById("heart").src == "heart_filled.png") {
    document.getElementById("heart").src = "heart_empty.png";
    }
}

Also, because you only have two statements, you can simply use else instead of else if.此外,因为您只有两个语句,所以您可以简单地使用 else 而不是 else if。

function func() {
    if (document.getElementById("heart").src == "heart_empty.png")  {
    document.getElementById("heart").src = "heart_filled.png";
    } else (document.getElementById("heart").src == "heart_filled.png") {
    document.getElementById("heart").src = "heart_empty.png";
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM