在JavaScript中将文本替换为时间

时间:2017-02-23 01:35:29

标签: javascript

需要帮助

  1. 单击任何链接时,文本(未单击)应替换为时间。

    <div><a href="http://facebook.com">Facebook(NOT CLICKED)</a></div>
    <div>
        <ul>
            <li><a href="http://youtube.com">Youtube(NOT CLICKED)</a></li>
        </ul>
    

1 个答案:

答案 0 :(得分:1)

var links = document.querySelectorAll("a");
links.forEach(function(l) {
  l.addEventListener("click", clickHandler);
});

function clickHandler(ev) {
  ev.preventDefault(); // don't redirect (don't go the the link in the href)
  
  this.textContent = this.textContent.replace("(NOT CLICKED)", "(" + Date() + ")"); // replace the text "(NOT CLICKED)" with "(date)"
}
<div><a href="http://facebook.com">Facebook(NOT CLICKED)</a></div>
<div>
  <ul>
    <li><a href="http://youtube.com">Youtube(NOT CLICKED)</a></li>
  </ul>
</div>