App下載
首頁javascriptanchorJavascript Element - 如何獲取點(diǎn)擊的錨文本

Javascript Element - 如何獲取點(diǎn)擊的錨文本

我們想知道如何獲取點(diǎn)擊的錨文本。

<!DOCTYPE html>
<html>
<body>
  <div id="bbtop"></div>
  <div id="bbdefine"></div>
  <ul class="dl-submenu">
    <li><a>AAAAA</a></li>
    <li><a>BBBBB</a></li>
    <li><a>test</a></li>
    <li><a>w3cschool.cn</a></li>
  </ul>
  <script>
    var bbdefine = document.getElementById('bbtop');
    var elems = document.querySelectorAll('.dl-submenu li a');
    for (var i=0; i<elems.length; ++i) {
      elems[i].addEventListener('click', clickFunc);
    }
    function clickFunc(event) {
      bbdefine.textContent = this.textContent+' Capacitor';
    }
    </script>
</body>
</html>