2013-04-10 66 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); 
    }); 
相关问题