如何在<a> tag

时间:2017-12-09 14:32:03

标签: javascript html increment

Hi I want to increment the value within an and increment it by 1 every time a button is clicked. The code i have so far is shown below:

<a data-role="button" data-icon="shop" class="ui-btn-right ui-shadow ui-corner-all ui-btn-icon-center ui-icon-heart" id="Count" style="text-align: center;">0</a>

<script>


btn.onclick = function() {
  var linkText = parseInt(document.getElementById('Count').value);
  value = isNaN(value) ? 0 : value;
  value++;
  document.getElementById('Count').value = value;
  }
</script>

however it does not update the value of <a></a> once I press the button. I want to be able to change the value of <a> tag for example <a>0</a> and once the button is pressed <a> tag will display <a>1</a>

1 个答案:

答案 0 :(得分:1)

使用textContent代替value,这是为输入元素保留的属性。

var count = document.getElementById('Count');

document.getElementById('button').onclick = function() {
  var linkText = +count.textContent;

  count.textContent = ++linkText;
}
<button id="button">Increment</button>

<a data-role="button" id="Count">0</a>