[英]How come my custom Javascript code is working on one page of my Wordpress site but not on others?
I am using Wordpress to create a doll artist site and for each doll I have created a custom .php
template. 我正在使用Wordpress创建一个玩偶艺术家网站,并且为每个玩偶创建了一个自定义
.php
模板。 For example, for the Cher doll I have created cher.php
. 例如,对于Cher娃娃,我创建了
cher.php
。 Then in wp admin I have created a page called cher and in Page Attributes > Template I have selected cher as the template. 然后在wp admin中,我创建了一个名为cher的页面,并在Page Attributes> Template中选择cher作为模板。 So when I go to the page http://mitemiu.com/cher the cher doll template
cher.php
is loaded. 因此,当我转到http://mitemiu.com/cher页面时,将加载cher娃娃模板
cher.php
。 That page works fine. 该页面工作正常。
The cher.php
calls the header.php
first via the get_header();
cher.php
首先通过get_header();
调用header.php
get_header();
function. 功能。 In the
header.php
function I link to jquery-1.5.min.js
, jquery.galleriffic.js
and mitemiu.js
. 在
header.php
函数中,我链接到jquery-1.5.min.js
, jquery.galleriffic.js
和mitemiu.js
。
Gallerific 2.0 is a jQuery based gallery plug-in and mitemiu.js
is my custom Javascript file which contains: Gallerific 2.0是一个基于jQuery的图库插件,
mitemiu.js
是我的自定义Javascript文件,其中包含:
toggleSlider()
for displaying/hiding of a doll selection menu toggleSlider()
用于显示/隐藏玩偶选择菜单 toggleSlider()
when the a
element containing the title doll
is clicked doll
a
元素时要运行toggleSlider()
语句 Relevant files ( jquery-1.5.min.js
, jquery.galleriffic.js
remain pure & untouched by me) 相关文件 (
jquery-1.5.min.js
, jquery.galleriffic.js
仍然是纯净的,对我jquery.galleriffic.js
是不变的)
mitemiu.js
code http://pastebin.com/sBZg7vA2 mitemiu.js
代码http://pastebin.com/sBZg7vA2 header.php
http://pastebin.com/5ybzxBbM header.php
http://pastebin.com/5ybzxBbM cher.php
http://pastebin.com/tBE7f8YZ cher.php
http://pastebin.com/tBE7f8YZ When you go to http://mitemiu.com/cher the page loads and works fine. 当您访问http://mitemiu.com/cher时 ,页面将加载并正常运行。 When you click on the dolls link in the top navigation bar the
#doll-menu
DIV slides down/fades in via the toggleSlider()
function. 单击顶部导航栏中的玩偶链接时,
#doll-menu
toggleSlider()
#doll-menu
DIV将通过toggleSlider()
函数向下滑动/淡入。 Click it again and the #doll-menu
DIV fades out/slides up. 再次单击它,
#doll-menu
DIV淡出/向上滑动。
All is fine but, when you go to say any other page on the site (blog, order, about, contact) and then try clicking the doll link, BOOM! 一切都很好,但是当您说网站上的任何其他页面(博客,订单,关于,联系方式) ,然后尝试单击娃娃链接BOOM时! It doesn't work.
没用
How come the doll link only works on all doll pages and not on other pages? 娃娃链接为什么只在所有娃娃页面上起作用而在其他页面上不起作用?
Is there some sort of JS conflict occuring? 是否发生某种JS冲突?
The href attribute of all the other links are right. 所有其他链接的href属性都是正确的。 However, the href attrubte of the doll page is "#", which just redirects to the top of the page.
但是,洋娃娃页面的href属性是“#”,它仅重定向到页面顶部。
So on all the doll pages, it seems right because it goes to the top of the doll page. 因此,在所有洋娃娃页面上,这似乎都是正确的,因为它到达了洋娃娃页面的顶部。 But on all other pages, it just goes to the top of itself.
但是在所有其他页面上,它只是达到了最高点。 On the other pages, just fix the href attribute of the doll link.
在其他页面上,只需修复玩偶链接的href属性。
it is not working on other pages because in your mitemiu.js
the first function assumes the existence of div id thumbs
which does not exist outside of the dolls page: 它在其他页面上不起作用,因为在您的
mitemiu.js
,第一个函数假定存在div id thumbs
,而在娃娃页面之外不存在div id thumbs
:
jQuery(document).ready(function($) {
var gallery = $('#thumbs').galleriffic({ ...
It is not finding a div with id " thumbs
" and the rest of the JS is not being loaded. 它找不到ID为“
thumbs
”的div,并且未加载其余的JS。 If you remove this top function from the other pages it works just fine: http://jsfiddle.net/FdqxS/ 如果您从其他页面中删除此顶级功能,则效果很好: http : //jsfiddle.net/FdqxS/
Or, if you don't want separate JS files, check for the div using: 或者,如果您不想使用单独的JS文件,请使用以下命令检查div:
if($('#thumbs').length > 0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.