2013-04-10 11 views
1
  • index.html的 - 有菜單category1category2category3
  • product.html - 具有功能changeIframe([category1 | category2 | category3]);

當我點擊 「的index.html」 頁面上的菜單,鏈接會帶我去「product.html」頁面立即調用功能changeIframe(var)changeIframe(var)函數將比較$var(值爲var後跟我單擊的菜單名稱),然後更改此頁面中的內容。我該如何做這樣的事情?如何點擊鏈接頁面上的鏈接並調用功能同時進行?

+0

我想你會需要使用PHP或其他一些編程語言,否則你可能有爆炸的網址在js – Class 2013-04-10 04:54:50

回答

2

只是一個想法index.phtml頁,將該類別作爲Url Hash傳遞。

的index.html:

<a href="product.html#category1">Category 1</a> 
<a href="product.html#category2">Category 2</a> 
<a href="product.html#category3">Category 3</a> 

product.html

var category = (location.hash).replace('#',''); 
changeIframe(category); //call function 

如果只有你需要使用JavaScript。如果使用服務器端腳本,它會更容易。

+0

這是個好主意。你的意思是運行函數onload和檢查條件,是吧? – fronthem 2013-04-10 06:13:23

+0

是的。祝你好運 !。 – egig 2013-04-10 06:15:59

0

在,當你點擊鏈接通菜單名稱作爲查詢字符串,當你到達product.phtml呼籲$的changeiframe(VAR)(document).ready函數

1

在你的鏈接上添加類別作爲主題標籤並閱讀。

<a href="product.html#category1">menu link</a> 

在你的產品頁面

window.onload = function(){ 

    var category = (window.location.hash).replace("#",'');; 
    alert(category); 
    // changeIframe(category) 

} 

,並刪除了 「#」 像@Charlie節目。

0

使用PHP(如果您有一個支持它的服務器)的一種方法。

如果你最終得到的PHP設置,這裏是一個選項:

的index.php:

<a href="product.php?cat=1">Category 1</a> 
<a href="product.php?cat=1">Category 2</a> 
<a href="product.php?cat=1">Category 3</a> 

然後在product.php使用此代碼與你的Javascript,以獲得類別傳遞:

changeIframe(<?php $_GET['cat'] ?>) 
+0

如果你使用PHP,那麼你在這種情況下用'$ _GET ['cat']'獲取變量。 – Daniel 2013-04-10 05:12:23

+0

對不起,對,你是對的 – 2013-04-10 05:19:08

1

的index.html

<a href="product.html#category1">category 1</a> 
<a href="product.html#category2">category 2</a> 
<a href="product.html#category3">category 3</a> 

product.html

$(document).ready(function(){ 
    var item = toLowerCase((document.location.hash)).replace('#',''); 
    if(/category[1-3]/i.test(item)){ 
     changeIframe(item); 
    }else{ 
     changeIframe(defaultitem); 
    } 
}); 

如果你正在使用PHP

的index.html

<a href="product.html?item=category1">category 1</a> 
<a href="product.html?item=category2">category 2</a> 
<a href="product.html?item=category3">category 3</a> 

產品。作爲查詢字符串HTML

<script> 
<?php 
if(isset($_GET['item']) && preg_match("/category[1-3]/", $_GET['item'])){ 
    echo "changeIframe(" . $_GET['item'] .");"; 
}else{ 
    echo "changeIframe(defaultitem);"; 
} 
</script> 
0

通鏈接product.html?category=1,並且可以在頁面中使用此代碼

function getUrlVars() 
    { 
     var vars = [], hash; 
     var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&'); 
     for(var i = 0; i < hashes.length; i++) 
     { 
      hash = hashes[i].split('='); 
      vars.push(hash[0]); 
      vars[hash[0]] = hash[1]; 
     } 
     return vars; 

    } 



    $(document).ready(function() { 

    var id = getUrlVars()["category"]; 
    changeIframe(var); 
    }); 
相關問題