2016-10-03 67 views
0

我有產品網格,每個產品都會調用JavaScript以及用於打開iframe窗口的唯一ID。 當調用第一個函數時它工作正常,但是在調用第二個函數時它什麼也不做。 我將有大約20種需要調用該腳本的產品。2個或更多功能無法在Javascript中執行

我想如果可能的時候調用第二個iframe,第一個關閉,第二個出現等。

下面是腳本:

function init() { 

    test = true; 

    document.getElementById('go<?php echo $product['product_id']; ?>').onclick = function() { 
    ifr = document.createElement('iframe'); 
    ifr.setAttribute('src', this.href); 
    ifr.src = this.href; 
    ifr.setAttribute('id', 'iframe_a<?php echo $product['product_id']; ?>'); 

    if (test == true) { 
     ifr.onreadystatechange = function() { 
     if (this.readyState == 'complete<?php echo $product['product_id']; ?>') {} 
     } 
     document.getElementById('iframe_a_container<?php echo $product['product_id']; ?>').appendChild(ifr); 
     test = false; 
    } 
    return false; 
    } 
} 

window.addEventListener ? 
    window.addEventListener('load', init, false) : 
    window.attachEvent('onload', init); 

的iframe代碼:

<div id="iframe_a_container<?php echo $product['product_id']; ?>"> </div> 

和按鈕的代碼:

<a id="go<?php echo $product['product_id']; ?>" href="index.php?route=product/quickview&amp;product_id=<?php echo $product['product_id']; ?>">Quick View</a> 

回答

0

我假設你聲明(的IFR前外的onclick);如果沒有,你應該這樣做(讓你有一個參考來訪問它)和嘗試,如果此修改的onclick工作:

document.getElementById('go<?php echo $product['product_id']; ?>').onclick = function() { 
    $(ifr).remove(); // Remove it from the screen 
    ifr = null; 
    ifr = document.createElement('iframe'); 
    ifr.setAttribute('src', this.href); 
    ifr.src = this.href; 
    ifr.setAttribute('id', 'iframe_a<?php echo $product['product_id']; ?>'); 

    if (test == true) { 
     ifr.onreadystatechange = function() { 
     if (this.readyState == 'complete<?php echo $product['product_id']; ?>') {} 
     } 
     document.getElementById('iframe_a_container<?php echo $product['product_id']; ?>').appendChild(ifr); 
     test = false; 
    } 
    return false; 
    } 
+0

用你的方法,該按鈕只是就像沒有功能的HREF。 該腳本停止工作。它位於onclick內部。這裏是工作腳本http://www.petite-style.com/tea(產品下方的快速查看按鈕) –

+0

用onmouseover嘗試過,它可以與你的代碼一起修改,但它不能完成這項工作。 –