2015-05-09 181 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'放在你的例子中? –