Javascript not loading wordpress footer

2019-07-20 19:51发布

I recently started learning Javascript. I am trying to add a small script to the footer of a page on my Wordpress site. I am using the "insert headers and footers" plugin. However, the script does not seem to load. I do not believe it is a syntax issue, since a similar script works on a different site. However, I cannot figure out what is different here. When looking at the inserted script in chrome inspector, inside of the script seems to be just plain text (not colored javascript code). Is there something I can do to fix this and make the script run?

Link to the page: http://memories.uvaphotography.com/home/services/

Javascript code I am trying to insert:

<script>
$("#reply-title").hide();
</script>

2条回答
▲ chillily
2楼-- · 2019-07-20 20:43

The reason that you're not seeing your script execute is because jQuery isn't assigned to the $ variable.

When viewing the element inspector console on your page there's a javascript error

Uncaught TypeError: undefined is not a function

When I run the same script via console replacing $ with jQuery the #reply-title is hidden successfully.

查看更多
时光不老,我们不散
3楼-- · 2019-07-20 20:53

jQuery in WordPress runs in noConflict mode which means the global $ shortcut for jQuery isn't available. Replace your code with the following:

<script>
    jQuery(document).ready(function($) {
        $("#reply-title").hide();
    });
</script>

Inside the document ready wrapper the $ can be used as an alias for jQuery.

查看更多
登录 后发表回答