简体   繁体   English

我的自定义Javascript代码如何在我的Wordpress网站的一页上而不在其他页面上起作用?

[英]How come my custom Javascript code is working on one page of my Wordpress site but not on others?

Current Set Up 当前设置

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.jsjquery.galleriffic.jsmitemiu.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文件,其中包含:

  1. Gallerific custom settings (image transition speed, UI components to be loaded, etc) Gallerific自定义设置(图像过渡速度,要加载的UI组件等)
  2. Custom function called toggleSlider() for displaying/hiding of a doll selection menu 自定义函数toggleSlider()用于显示/隐藏玩偶选择菜单
  3. A statement to run 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.jsjquery.galleriffic.js仍然是纯净的,对我jquery.galleriffic.js是不变的)


Problem 问题

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.

相关问题 我的自定义页面上的Wordpress分页不再起作用 - Wordpress Pagination is not working anymore in my custom page 如何在Wordpress中为自定义表单组织代码? - How to organize my code for a custom form in Wordpress? 如何使用自定义登录脚本访问我的Wordpress网站 - How to use a custom login script to access my wordpress site 如何将我的wordpress网站主页的所有标题居中? - How to centre allign heading of my home page of wordpress site? 如何删除我的WordPress网站中动态插入的代码? - How to remove dynamically inserted code in my WordPress site? 在我的WordPress网站中找到可疑代码 - 如何解决? - Suspicious code found in my WordPress site - How to fix? 如何将恶意代码添加到我的WordPress网站? - How is malicious code in being added to my WordPress site? 为什么我的Javascript警报无法在此自定义WordPress插件上运行? - Why is my Javascript alert not working on this custom WordPress plugin? 联系表格 7 不在我的 wordpress 站点中? 但在空白页上工作我的代码有一些问题 - the contact form 7 is not in my wordpress site? but works on a blank page some issues with my code 我的Wordpress网站上的jQuery脚本均无法正常工作 - None of the jQuery scripts on my Wordpress site are working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM