gpt4 book ai didi

javascript - 通过script.js在wordpress中包含javascript多个文件

转载 作者:行者123 更新时间:2023-11-30 20:32:23 34 4
gpt4 key购买 nike

我有用于 html 到 wordpress 转换的 script.js 文件。在 html 中它有效,但在 wordpress 中它不起作用。此文件包含 slider 、菜单等,我启用了在没有此文件的情况下将 html 转换为 wordpress,该文件具有以下内容

function include(scriptUrl) {
document.write('<script src="' + scriptUrl + '"></script>');
}

function isIE() {
var myNav = navigator.userAgent.toLowerCase();
return (myNav.indexOf('msie') != -1) ? parseInt(myNav.split('msie')[1]) : false;
};

/* cookie.JS
========================================================*/
include('js/jquery.cookie.js');

/* Easing library
========================================================*/
include('js/jquery.easing.1.3.js');

/* Stick up menus
========================================================*/
;
(function ($) {
var o = $('html');
if (o.hasClass('desktop')) {
include('js/tmstickup.js');

$(document).ready(function () {
$('#stuck_container').TMStickUp({})
});
}
})(jQuery);

/* ToTop
========================================================*/
;
(function ($) {
var o = $('html');
if (o.hasClass('desktop')) {
include('js/jquery.ui.totop.js');

$(document).ready(function () {
$().UItoTop({
easingType: 'easeOutQuart',
containerClass: 'toTop fa fa-arrow-up'
});
});
}
})(jQuery);

/* EqualHeights
========================================================*/
;
(function ($) {
var o = $('[data-equal-group]');
if (o.length > 0) {
include('js/jquery.equalheights.js');
}
})(jQuery);

/* SMOOTH SCROLLIG
========================================================*/
;
(function ($) {
var o = $('html');
if (o.hasClass('desktop')) {
include('js/jquery.mousewheel.min.js');
include('js/jquery.simplr.smoothscroll.min.js');

$(document).ready(function () {
$.srSmoothscroll({
step: 150,
speed: 800
});
});
}
})(jQuery);

/* Copyright Year
========================================================*/
;
(function ($) {
var currentYear = (new Date).getFullYear();
$(document).ready(function () {
$("#copyright-year").text((new Date).getFullYear());
});
})(jQuery);


/* Superfish menus
========================================================*/
;
(function ($) {
include('js/superfish.js');
})(jQuery);

/* Navbar
========================================================*/
;
(function ($) {
include('js/jquery.rd-navbar.js');
})(jQuery);

/* Camera
========================================================*/
;(function ($) {
var o = $('#camera');
if (o.length > 0) {
if (!(isIE() && (isIE() > 9))) {
include('js/jquery.mobile.customized.min.js');
}

include('js/camera.js');

$(document).ready(function () {
o.camera({
autoAdvance: true,
height: '25.8536%',
minHeight: '300px',
pagination: true,
thumbnails: false,
playPause: false,
hover: false,
loader: 'none',
navigation: false,
navigationHover: false,
mobileNavHover: false,
fx: 'simpleFade'
})
});
}
})(jQuery);

/* FancyBox
========================================================*/
;(function ($) {
var o = $('.thumb');
if (o.length > 0) {
include('js/jquery.fancybox.js');
include('js/jquery.fancybox-media.js');
include('js/jquery.fancybox-buttons.js');
$(document).ready(function () {
o.fancybox();
});
}
})(jQuery);

/* Parallax
=============================================*/
;(function ($) {
include('js/jquery.rd-parallax.js');
})(jQuery);


/* Google Map
========================================================*/
;
(function ($) {
var o = document.getElementById("google-map");
if (o) {
include('//maps.google.com/maps/api/js?sensor=false');
include('js/jquery.rd-google-map.js');

$(document).ready(function () {
var o = $('#google-map');
if (o.length > 0) {
var styleArray = [
{
"featureType": "administrative.locality",
"elementType": "all",
"stylers": [
{
"hue": "#2c2e33"
},
{
"saturation": 7
},
{
"lightness": 19
},
{
"visibility": "on"
}
]
},
{
"featureType": "landscape",
"elementType": "all",
"stylers": [
{
"hue": "#ffffff"
},
{
"saturation": -100
},
{
"lightness": 100
},
{
"visibility": "simplified"
}
]
},
{
"featureType": "poi",
"elementType": "all",
"stylers": [
{
"hue": "#ffffff"
},
{
"saturation": -100
},
{
"lightness": 100
},
{
"visibility": "off"
}
]
},
{
"featureType": "road",
"elementType": "geometry",
"stylers": [
{
"hue": "#bbc0c4"
},
{
"saturation": -93
},
{
"lightness": 31
},
{
"visibility": "simplified"
}
]
},
{
"featureType": "road",
"elementType": "labels",
"stylers": [
{
"hue": "#bbc0c4"
},
{
"saturation": -93
},
{
"lightness": 31
},
{
"visibility": "on"
}
]
},
{
"featureType": "road.arterial",
"elementType": "labels",
"stylers": [
{
"hue": "#bbc0c4"
},
{
"saturation": -93
},
{
"lightness": -2
},
{
"visibility": "simplified"
}
]
},
{
"featureType": "road.local",
"elementType": "geometry",
"stylers": [
{
"hue": "#e9ebed"
},
{
"saturation": -90
},
{
"lightness": -8
},
{
"visibility": "simplified"
}
]
},
{
"featureType": "transit",
"elementType": "all",
"stylers": [
{
"hue": "#e9ebed"
},
{
"saturation": 10
},
{
"lightness": 69
},
{
"visibility": "on"
}
]
},
{
"featureType": "water",
"elementType": "all",
"stylers": [
{
"hue": "#e9ebed"
},
{
"saturation": -78
},
{
"lightness": 67
},
{
"visibility": "simplified"
}
]
}
]

o.googleMap({
styles: styleArray,
});
}
});
}
})
(jQuery);

(function ($) {
var o = document.getElementById("google-map2");
if (o) {
include('//maps.google.com/maps/api/js?sensor=false');
include('js/jquery.rd-google-map.js');

$(document).ready(function () {
var o = $('#google-map2');
if (o.length > 0) {
var styleArray = [
{
"featureType": "administrative.locality",
"elementType": "all",
"stylers": [
{
"hue": "#2c2e33"
},
{
"saturation": 7
},
{
"lightness": 19
},
{
"visibility": "on"
}
]
},
{
"featureType": "landscape",
"elementType": "all",
"stylers": [
{
"hue": "#ffffff"
},
{
"saturation": -100
},
{
"lightness": 100
},
{
"visibility": "simplified"
}
]
},
{
"featureType": "poi",
"elementType": "all",
"stylers": [
{
"hue": "#ffffff"
},
{
"saturation": -100
},
{
"lightness": 100
},
{
"visibility": "off"
}
]
},
{
"featureType": "road",
"elementType": "geometry",
"stylers": [
{
"hue": "#bbc0c4"
},
{
"saturation": -93
},
{
"lightness": 31
},
{
"visibility": "simplified"
}
]
},
{
"featureType": "road",
"elementType": "labels",
"stylers": [
{
"hue": "#bbc0c4"
},
{
"saturation": -93
},
{
"lightness": 31
},
{
"visibility": "on"
}
]
},
{
"featureType": "road.arterial",
"elementType": "labels",
"stylers": [
{
"hue": "#bbc0c4"
},
{
"saturation": -93
},
{
"lightness": -2
},
{
"visibility": "simplified"
}
]
},
{
"featureType": "road.local",
"elementType": "geometry",
"stylers": [
{
"hue": "#e9ebed"
},
{
"saturation": -90
},
{
"lightness": -8
},
{
"visibility": "simplified"
}
]
},
{
"featureType": "transit",
"elementType": "all",
"stylers": [
{
"hue": "#e9ebed"
},
{
"saturation": 10
},
{
"lightness": 69
},
{
"visibility": "on"
}
]
},
{
"featureType": "water",
"elementType": "all",
"stylers": [
{
"hue": "#e9ebed"
},
{
"saturation": -78
},
{
"lightness": 67
},
{
"visibility": "simplified"
}
]
}
]

o.googleMap({
styles: styleArray,
});
}
});
}
})
(jQuery);

/* WOW
========================================================*/
;
(function ($) {
var o = $('html');

if ((navigator.userAgent.toLowerCase().indexOf('msie') == -1 ) || (isIE() && isIE() > 9)) {
if (o.hasClass('desktop')) {
include('js/wow.js');

$(document).ready(function () {
new WOW().init();
});
}
}
})(jQuery);

/* Contact Form
========================================================*/
;
(function ($) {
var o = $('#contact-form');
if (o.length > 0) {
include('js/modal.js');
include('js/TMForm.js');

if($('#contact-form .recaptcha').length > 0){
include('../www.google.com/recaptcha/api/js/recaptcha_ajax.js');
}
}
})(jQuery);

/* Search.js
========================================================*/
;
(function ($) {
include('js/TMSearch.js');
})(jQuery);

/* Orientation tablet fix
========================================================*/
$(function () {
// IPad/IPhone
var viewportmeta = document.querySelector && document.querySelector('meta[name="viewport"]'),
ua = navigator.userAgent,

gestureStart = function () {
viewportmeta.content = "width=device-width, minimum-scale=0.25, maximum-scale=1.6, initial-scale=1.0";
},

scaleFix = function () {
if (viewportmeta && /iPhone|iPad/.test(ua) && !/Opera Mini/.test(ua)) {
viewportmeta.content = "width=device-width, minimum-scale=1.0, maximum-scale=1.0";
document.addEventListener("gesturestart", gestureStart, false);
}
};

scaleFix();
// Menu Android
if (window.orientation != undefined) {
var regM = /ipod|ipad|iphone/gi,
result = ua.match(regM);
if (!result) {
$('.sf-menus li').each(function () {
if ($(">ul", this)[0]) {
$(">a", this).toggle(
function () {
return false;
},
function () {
window.location.href = $(this).attr("href");
}
);
}
})
}
}
});
var ua = navigator.userAgent.toLocaleLowerCase(),
regV = /ipod|ipad|iphone/gi,
result = ua.match(regV),
userScale = "";
if (!result) {
userScale = ",user-scalable=0"
}
document.write('<meta name="viewport" content="width=device-width,initial-scale=1.0' + userScale + '">');

我尝试了很多方法。我应该如何将其包含在 wordpress 中以正确的方式工作?

最佳答案

如果您还可以从 PHP 加载脚本,则应使用以下函数:https://developer.wordpress.org/reference/functions/wp_enqueue_script/

否则,如果您必须从您的 javascript 中加载它们,您可以使用以下方法。您正在使用 jQuery,因此您可以使用以下 jQuery 函数来加载(不一定要执行):

$.getScript("script.js", function(){});

在此堆栈帖子中查看更多信息:https://stackoverflow.com/a/950146/5567106

关于javascript - 通过script.js在wordpress中包含javascript多个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50199558/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com