2015-05-09 131 views
0

我有这样的js代码(保存为shomz.js文件)寄存器JS

var shown = true; 
var parent = document.querySelector('.parent'); 
var child = document.querySelector('.child'); 

parent.addEventListener('mouseenter', function(){ 
    child.style.opacity = shown ? 0 : 1; 
    shown = !shown; 
}); 

js涉及以下css

* { 
    margin: 0; 
    padding: 0; 
} 

.parent { 
    width: 100%; 
    margin: 10px auto; 
    position: relative; 
} 

.child { 
    position: absolute; 
    top: 0; 
    width: 100%; 
    height: 100%; 
    display: block; 
    overflow: hidden; 
    transition: opacity 0.5s linear; 
} 


p { 
    padding: 1em; 
} 

html

<div class="parent"> 
<img src="http://www.fundraising123.org/files/u16/bigstock-Test-word-on-white-keyboard-27134336.jpg" alt="" width="500px" height="auto" /> 

<div class="child"> 
<img src="http://maui.hawaii.edu/tlc/wp-content/uploads/sites/53/2013/11/testing.jpg" alt="" width="500px" height="auto" /> 

从下面的js登记程序通过IDscript-calls.php(由Robin制造)单个页面开始,我想知道什么是调整代码,以定义多个网页的最佳方式(2 ,3,N)还通过ID,应用上述shomz.js代码。我没有兴趣找到一个会影响整个网站页面的全局规则,但只有通过ID页面定义;

// Main Scripts 
function register_js() { 

    if (!is_admin()) { 
     $url_prefix = is_ssl() ? 'https:' : 'http:'; 

     /* Get id of current page*/ 
     $page_id  = get_queried_object_id(); 
     /* Compare the desired page's id with the current page's id, if  they match, enqueue shomz*/ 
     if(YOUR_ID == $page_id){ 
      wp_register_script('shomz', THB_THEME_ROOT . '/assets/js/plugins/shomz.js', 'jquery', null, TRUE); 
      wp_enqueue_script('shomz'); 
     } 
     wp_localize_script('app', 'themeajax', array('url' => admin_url('admin-ajax.php'))); 
    } 
} 

回答

1

定义一个包含特定ID的数组。在if语句中,您只需检查当前的ID是否在您定义的数组中。

// Main Scripts 
function register_js() { 

    if (!is_admin()) { 
     $url_prefix = is_ssl() ? 'https:' : 'http:'; 

     /* Get id of current page*/ 
     $page_id  = get_queried_object_id(); 
     /* Compare the desired page's id with the current page's id, if  they match, enqueue shomz*/ 
     $ids = array(1, 2, 3, 4); 
     if (in_array($page_id, $ids)){ 
      wp_register_script('shomz', THB_THEME_ROOT . '/assets/js/plugins/shomz.js', 'jquery', null, TRUE); 
      wp_enqueue_script('shomz'); 
     } 
     wp_localize_script('app', 'themeajax', array('url' => admin_url('admin-ajax.php'))); 
    } 
} 

PHP参考:http://php.net/manual/en/function.in-array.php

+0

Vielen丹克罗宾,我敢肯定,这将工作,但我要读第一弄清楚如何我:)我不懒,在PHP的东西只是菜鸟。 –

+0

绝对没问题;) –

+0

据我所看到的,你的代码是从'在-array' documentation.In这种情况下,让我们说,我有3个ID的'2465','2466'以及第一例子类似' 2480'.I'll具有与每个'Id'一个接一个叠一个线对另一个与我的ID和...再次'$ page_id'更换'1','2','3'号码?我知道这听起来很愚蠢,我只是想弄清楚。你可以这么善良,请把我的'ID'放在你的例子中? –