简体   繁体   English

Javascript更改标签的innerhtml

[英]Javascript change innerhtml of a tag

I am trying to change the text of the innerhtml depending on the current value. 我试图根据当前值更改innerhtml的文本。 However, it isn't going in the if statements. 但是,它不在if语句中。

//HTML // HTML

<a class="btn" value="notadded" onclick="changetext(this.id,this.value)" id="wishlist">Add to wish list<i class="fa fa-heart-o"></i></a>

//Script //脚本

<script>function changetext(id,value) {
      if ( value == "notadded") {
        document.getElementById(id).innerHTML = "Added to Wishlist";
        document.getElementById(id).value = "added";
      } else if (value == "added")
      {
          document.getElementById(id).innerHTML= "Add to Wishlist";
          document.getElementById(id).value = "notadded";
      }
}
</script>

This issue here is that you cannot use value to get the text on that tag. 这里的问题是,您不能使用value来获取该标签上的文本。

You're right there, only one small change: 您就在那里,只有一个小小的变化:

Working Demo: JsFiddle 工作演示: JsFiddle

<a class="btn" value="notadded" onclick="changetext(this.id,this.value)" id="wishlist">Add to wish list<i class="fa fa-heart-o"></i></a>


<script>function changetext(id,value) {

        document.getElementById(id).innerHTML = "Added to Wishlist";
        document.getElementById(id).innerHTML = value;

}
changetext("wishlist");
</script>

Anchor tags do not have a value attribute 定位标记没有value属性

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a https://developer.mozilla.org/zh-CN/docs/Web/HTML/Element/a

However you could use a data attribute to do what you are doing. 但是,您可以使用data属性执行操作。

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

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