Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
106 views
in Technique[技术] by (71.8m points)

php - How to Make a Basic Snippet of Javascript Work in Wordpress

I found a snippet of Javascript online... that does exactly what I need to solve a problem. BUT I have not been able to get it to work without errors on Worpress.

The snippet is from here: https://www.the-art-of-web.com/javascript/remove-anchor-links/

<script type="text/javascript">

// Original JavaScript code by Chirp Internet: www.chirp.com.au
// Please acknowledge use of this code by including this header.

  document.querySelectorAll("a").forEach(function(current) {
    if(!current.hash) return;
    if(current.origin + current.pathname != self.location.href) return;
    (function(anchorPoint) {
      if(anchorPoint) {
        current.addEventListener("click", function(e) {
          anchorPoint.scrollIntoView({behavior: "smooth"});
          e.preventDefault();
        }, false);
      }
    })(document.querySelector(current.hash));
  });

</script>

I have this set up as it's own .js file in the functions.php (and then am enqeueing it via functions.php). It works (perfectly)... but it breaks other functionality.

Any help here would be GREATLY appreciated!

K

question from:https://stackoverflow.com/questions/65847445/how-to-make-a-basic-snippet-of-javascript-work-in-wordpress

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...