简体   繁体   English

从外部调用JavaScript无法正常工作

[英]Calling javascript externally not working

I am trying to load this javascript externally, but it seems to not be working? 我正在尝试从外部加载此javascript,但似乎无法正常工作? Is there a reason for this? 是否有一个原因?

I have been reading around, and I am loading jquery first before everything. 我一直在阅读,我在一切之前先加载jquery。 But when calling out to these functions they will not load. 但是,当调用这些函数时,它们将不会加载。

$(document).ready(function () {
  //add span to numbers
  var elem = document.getElementById('passage');
  elem.innerHTML = elem.innerHTML.replace(/\b(\d+)\b/g, '<span>$1</span>');

  //menu toggle     
  $("#social-wrap li:nth-child(6)").click(function () {
    $("#phone-nav").slideToggle("slow");
  });

  //nav slideDown      
  $('#main-nav li').hover(function () {
    //show its submenu
    $('ul', this).slideDown(100);
  }, function () {
    //hide its submenu
    $('ul', this).slideUp(100);
  });

  //show/hide countdown  
  $('#show').click(function () {
    $('#countdown').slideDown('slow');
    return false;
  });

  $('#hide').click(function () {
    $('#countdown').slideUp('fast');
    return false;
  });

  $('#hide,#show').click(function () {
    $('#hide,#show').toggle();
  })

});

EDIT 编辑

Here is the link to the wordpress theme I am building: http://beta.revival.tv/ 这是我正在构建的wordpress主题的链接: http : //beta.revival.tv/
As well as the JS file: http://beta.revival.tv/wp-content/themes/revival-theme/lib/scripts/main.js?ver=1.1 以及JS文件: http : //beta.revival.tv/wp-content/themes/revival-theme/lib/scripts/main.js?ver=1.1
Sorry for how messy the site is, still working on it. 很抱歉,该网站有多混乱,仍在处理中。

For one, I have heard that Wordpress messes with the $ shorthand for JQuery. 首先,我听说Wordpress弄乱了JQuery的$简写。 You may be able to wrap your code in something like this in order to be able to use the $ shorthand. 为了能够使用$简写形式,您可能可以将代码包装成这样的形式。 Otherwise try using the full jQuery() instead of $(). 否则,请尝试使用完整的jQuery()而不是$()。

( function( $ ) {
   // allows you to use $ inside of this wrapper.

} )( jQuery );

Also, how are you adding jQuery to your theme? 另外,如何将jQuery添加到主题中?

http://wordpress.org/support/topic/how-do-you-get-jquery-to-work-in-wordpress http://wordpress.org/support/topic/how-do-you-get-jquery-to-work-in-wordpress

 $('#hide,#show').click(function () {
    $('#hide,#show').toggle();
})

here semicolon is missing. 这里缺少分号。

Try it now again. 现在再试一次。 Hope it works. 希望它能工作。 Even if it doesn't ,post your main html page where you are calling it. 即使没有,也可以将您的主html页面发布在调用它的位置。

one really strange thing about including an external script is that it matters how you close it. 包含外部脚本的一件非常奇怪的事情是,关闭脚本的方式很重要。 This: 这个:

<script type='blah' src='path'></script>

works, and this: 工作,这:

<script type='blah' src='path' />

Doesn't. 不。 I don't know why, I only know this could happen. 我不知道为什么,我只知道这可能发生。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM