简体   繁体   English

Javascript和Wordpress页面加载

[英]Javascript and wordpress page load

Am working on a website techhouse.ng. 我正在网站techhouse.ng上工作。 My problem is that I have a javascript that changes the width and color of certain divs when the menu li links (services and contact) are clicked. 我的问题是,当单击菜单li链接(服务和联系人)时,我有一个javascript会更改某些div的宽度和颜色。 The script works fine but the problem is that by the time the page loads the divs reverts back to the default settings. 该脚本可以正常工作,但是问题在于,在页面加载时,div会恢复为默认设置。

I created page-service.php and page-contact.php for those links. 我为这些链接创建了page-service.php和page-contact.php。 but the script works on the index.php, so when you click on either the services link or the contact links the script works but by the time the page loads the divs revert back to their original css values. 但是脚本可以在index.php上运行,因此,当您单击服务链接或联系人链接时,脚本可以运行,但是在页面加载时,div会恢复为原始的CSS值。

 jQuery(document).ready(function ($) {

 $("#li-12").click(function () {
 $("#rcolumn").css("width", '25%');
$("#rcolumn").css("background-color", 'rgb(44, 62, 80)');
$(".wrapper").css("background-color", 'rgb(44, 62, 80)');
$(".nav li a").css("padding", '15px 0');
$(".nav li a").css("color", '#909090');
$(".nav li a").css("background", 'white');
$("#lcolumn").css("width", '75%');
$(".nav").css("width", '75%');
});

$("#li-13").click(function () {
$("#rcolumn").css("width", '25%');
$("#rcolumn").css("background-color", 'rgb(44, 62, 80)');
$(".wrapper").css("background-color", 'rgb(44, 62, 80)');
$(".nav li a").css("padding", '15px 0');
$(".nav li a").css("color", '#909090');
$(".nav li a").css("background", 'white');
$("#lcolumn").css("width", '75%');
$(".nav").css("width", '75%');
}); 

});

First, you can combine multiple selectors like this: $("#li-12, #li-13") so your can remove a lot of duplicate code. 首先,您可以像这样组合多个选择器: $("#li-12, #li-13")这样您就可以删除很多重复的代码。

Second, to get the styling on those 2 pages you could put the .css stuff inside a named function and call it... though it's far from ideal, as it will have to wait until jquery framework is loaded, meaning the user will first see the default style until jQuery kicks in. It could be done using pure javascript which would fire sooner. 其次,要在这两个页面上进行样式设置,您可以将.css内容放入一个命名函数中并对其进行调用……虽然这远非理想,因为它必须等到jquery框架加载完毕,这意味着用户将首先请参阅默认样式,直到jQuery启动为止。这可以使用纯JavaScript来完成,这将尽快启动。 A lot better would be to put style tags on those 2 templates, to override the default styles. 最好将样式标签放在这两个模板上,以覆盖默认样式。 Or use classes to do stuff. 或使用类做事情。

But if you don't mind the delay you could do something like this: 但是,如果您不介意延迟,可以执行以下操作:

jQuery(function($) { //.ready

  function altStyling() {
    $("#rcolumn").css("width", '25%');
    $("#rcolumn, .wrapper").css("background-color", 'rgb(44, 62, 80)');
    $(".nav li a").css("padding", '15px 0');
    $(".nav li a").css("color", '#909090');
    $(".nav li a").css("background", 'white');
    $("#lcolumn, .nav").css("width", '75%');
  }

  // click menu items
  $("#li-12, #li-13").click(altStyling);

  // call if we're on services or contact page
  if (location.pathname == "/services/" || location.pathname == "/contact/") altStyling();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM