【发布时间】:2014-02-22 00:45:21
【问题描述】:
我花了很长时间试图弄清楚这一点。我试图添加一个需要外部 jquerylibrary 的脚本。我可以通过在 scripttags 之间插入我的脚本来使其工作,但是我知道这不是正确的方法,它会破坏网站上的另一个脚本。
今晚我花了相当多的时间试图弄清楚如何正确添加脚本,但我就是不明白。
我知道这样的事情是让脚本入队的正确方法:
function my_scripts_method() {
wp_register_script( 'jquery', 'http://code.jquery.com/jquery-1.9.1.js');
wp_enqueue_script( 'jquery' );
}
add_action('wp_enqueue_scripts', 'my_scripts_method');
我的主要问题是,我如何编写函数以便它调用库,同时故障保护它只加载一次,并且不会与其他脚本崩溃?这是脚本:
$(document).ready(function () {
$("#menu-item-16").hover(
function () {
$('body').css("background", "#ff9900");
},
function () {
$('body').css("background", "#ccc");
}
);
$("#menu-item-17”).hover(
function () {
$('body').css("background", "red");
},
function () {
$('body').css("background", "#ccc");
}
);
$("#menu-item-18”).hover(
function () {
$('body').css("background", "yellow");
},
function () {
$('body').css("background", "#ccc");
}
);
});
编辑:
第二个问题,几个库和一个样式表。
如上所述,我有一个更复杂的脚本,你们也许可以帮助我。
我现在在标题中有这段代码,它可以工作。
`<link rel="stylesheet" type="text/css" href="/wp-content/themes/neighborhood/js/jquery.fullPage.css" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.9.1/jquery-ui.min.js"> </script>
<script type="text/javascript" src="/wp-content/themes/neighborhood/js/vendors/jquery.slimscroll.min.js"></script>
<script type="text/javascript" src="/wp-content/themes/neighborhood/js/jquery.fullPage.js"></script>
<script>
$(document).ready(function() {
$.fn.fullpage({
verticalCentered: true,
resize : true,
slidesColor : ['#AF1700', '#359973', '#F46321', '#A6C7DB'],
scrollingSpeed: 700,
easing: 'easeInQuart',
menu: false,
navigation: false,
navigationPosition: 'right',
navigationTooltips: ['firstSlide', 'secondSlide'],
slidesNavigation: true,
slidesNavPosition: 'bottom',
loopBottom: false,
loopTop: false,
loopHorizontal: true,
autoScrolling: true,
scrollOverflow: true,
css3: false,
paddingTop: '3em',
paddingBottom: '10px',
fixedElements: '#element1, .element2',
normalScrollElements: '#element1, .element2',
keyboardScrolling: true,
touchSensitivity: 15,
continuousVertical: false,
animateAnchor: false,
setScrollingSpeed: 1000,
});
});
</script>
`
根据我新发现的见解,我尝试了这个,但没有奏效:
`
function fullpage() { wp_enqueue_script('jquery');
wp_register_style( ’fullpage-css', get_template_directory_uri() . '/js/jquery.fullPage.css','','', 'screen' );
wp_register_script( 'jquery.1.8.3', get_template_directory_uri() . 'http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js', array('jquery'),'',true );
wp_register_script( 'jquery.1.9.1', get_template_directory_uri() . 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.9.1/jquery-ui.min.js', array('jquery'),'',true );
wp_register_script( 'fullpage', get_template_directory_uri() . '/js/jquery.fullPage.js', array('jquery'),'',true );
wp_register_script( 'fullpagecode', get_template_directory_uri() . '/js/fullpagecode.js', array('jquery'),'',true );
wp_register_script( 'slimscroll', get_template_directory_uri() . '/js/vendors/jquery.slimscroll.min.js', '', null,'' );
wp_enqueue_style( 'fullpage-css' ); // Enqueue our stylesheet
wp_enqueue_script( 'jquery.1.8.3' ); // Enqueue our first script
wp_enqueue_script( 'jquery.1.9.1' ); // Enqueue our second script
wp_enqueue_script( 'fullpage' ); // Enqueue our third script
wp_enqueue_script( 'fullpagecode' ); // Enqueue fourth script
wp_enqueue_script( ’slimscroll’ ); // Enqueue fifth script
}add_action( 'wp_enqueue_scripts', ’fullpage’ ); `
【问题讨论】:
-
这是您用于网站的代码?它有一些错字将
”替换为"
标签: javascript php jquery wordpress