Javascript jQuery在Wordpress中不工作
我有一段jQuery脚本,可以在这里找到: 当我将此脚本合并到wordpress模板中时,我无法工作 我已经检查了jquery源代码链接,重写了代码,没有任何效果Javascript jQuery在Wordpress中不工作,javascript,jquery,wordpress,Javascript,Jquery,Wordpress,我有一段jQuery脚本,可以在这里找到: 当我将此脚本合并到wordpress模板中时,我无法工作 我已经检查了jquery源代码链接,重写了代码,没有任何效果 <html> <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> </head> <body> <s
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
</head>
<body>
<script type="text/javascript">
$(document).ready(function() {
$(".move").toggle(
function() {
$("#teetimetag").animate({
right: "0px"
}, "normal");
},function() {
$("#teetimetag").animate({
right: "-200px"
}, "normal");
});
});
</script>
<div id="teetimetag">
<img class="move" src="http://test.tooheavytoskydive.com/wp-content/themes/newtheme/images/flag.png" style="float: left;" />
<div style="margin: 15px 0 0 50px;">
<a href="http://test.tooheavytoskydive.com/reservations/" style="color: #FFF; font-weight: bold; font-size: 17px;">Reserve your Tee-Time</a>
</div>
</div>
</body>
</html>
$(文档).ready(函数(){
$(“.move”).toggle(
函数(){
$(“#teetimetag”)。设置动画({
右:“0px”
}“正常”);
},函数(){
$(“#teetimetag”)。设置动画({
右:“-200px”
}“正常”);
});
});
您应该在template functions.php文件中注册js文件:
wp_register_script('jquery', 'http://code.jquery.com/jquery-1.7.2.min.js');
您的网站出现以下javascript错误:“$不是函数”。jQuery似乎与其他库冲突。这可以通过调用jQuery.noConflict()并用“jQuery”替换所有$来解决。代码如下所示:
jQuery.noConflict();
jQuery(document).ready(function() {
jQuery(".move").toggle(
function() {
jQuery("#teetimetag").animate({
right: "0px"
}, "normal");
},function() {
jQuery("#teetimetag").animate({
right: "-200px"
}, "normal");
});
});
作为一般规则,除非您使用了其中一个变量,否则不应为
jQuery
使用$
变量。下面是一个如何通过快捷方式jQuery安全使用$
变量的示例:
jQuery(function ($) {
/* You can safely use $ in this code block to reference jQuery */
});
要在wordpress上加载jquery,请使用以下两个版本之一。在插件页面
或function.php
function include_jQuery() {
if (!is_admin()) {
wp_enqueue_script('jquery');
}
}
add_action('init', 'include_jQuery');
当然,您可以使用任何有意义的函数名,而不是关闭include\jQuery
或者,您可以使用以下代码:
function include_jQuery() {
if (!is_admin()) {
// comment out the next two lines to load the local copy of jQuery
wp_deregister_script('jquery');
wp_register_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js', false, '1.8.3');
wp_enqueue_script('jquery');
}
}
add_action('init', 'include_jQuery');
你可能也会喜欢我个人学习上述技巧的链接。非常感谢埃里克·马丁这是使用免费版本的wordpress吗?他们是否限制添加你自己的js?这是免费版本。我从没听他们说过。我使用了大量使用它的插件。那个调用应该在哪里?在functions文件或页眉中?在您的functions.php中,将使用wp add_action()函数调用它,您可以控制如何以及为哪些页面调用它,请阅读wp文档的这一部分了解更多信息:关于找出冲突原因的任何解包工具/提示?我不知道有这样的工具。您可以禁用插件并查看问题所在。在您的案例中,问题似乎出现在预订日历脚本中,您在其中定义了“var jWPDev=jQuery.noConflict();”。使用noConflict时,$未设置。这就是出现错误“$不是函数”的原因。目前,$的功能被放入变量jWPDev中。您可以使用“jWPDev”而不是$,或者删除该行,使$成为原始函数。请记住,如果删除该行,则需要将所有出现的jWPDev更改为$(我认为这只用于文件wp content/plugins/booking/js/datepick/jquery.datepick.js的底部)。为了找到这一点,我只是浏览了您网站上的所有javascript,寻找可能导致冲突的原因(基本上是寻找代码中的$)。