gpt4 book ai didi

javascript - 将变量传递给另一个页面上的函数

转载 作者:行者123 更新时间:2023-12-03 04:06:25 25 4
gpt4 key购买 nike

我正在开发一个网站,并计划拥有它,以便某些链接将设置一个值,这将更改页面加载时显示的容器。我怎样才能让链接传递一个用于 onload 函数的值?

这是我的 HTML 代码的模型:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Lunch</title>
<link rel="stylesheet" type="text/css" href="style.css">
<script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
<script src="script.js"></script>
</head>
<body onload="navBar(); dateChange(); tabulate(0);">
<nav>
<ul>
<li><a href="appy.html">Appitizers</a></li>
<li><a href="break.html">Breakfast</a></li>
<li><a href="lunch.html">Lunch</a></li>
<li><a href="dinner.html">Dinner</a></li>
<li><a href="dessert.html">Dessert</a></li>
<li><a href="special.html">Ten-Course Dinner</a></li>
<li><a href="share.html">Send in your Recipes!</a></li>
</ul>
</nav>
<div class="main">
<div class="box">
<ul>
<li><a onclick="tabulate(this.id);" id="1">Chicken Clubhouse Sandwiches</a></li>
<li><a onclick="tabulate(this.id);" id="2">Smokey Tomato Soup</a></li>
</ul>
</div>

<div id="0" class="recipe" style="display: block;">
<div class="tabs">
<a class="tab">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</a>
</div>
<div class="page">
<p>The recipes you'll find here are ones you can use to impress guests at your next get together</p>
</div>
</div> <!--recipe card end-->
<div class="recipe" id="1">
<h1>Chicken Clubhouse Sandwiches</h1>
</div> <!--recipe card end-->
<div class="recipe" id="2">
<h1>Smokey Tomato Soup</h1>
</div> <!--recipe card end-->

</div>
</body>
</html>

这是我的制表函数:

function tabulate(tabNum){
$('.recipe').each(function() {
if(tabNum==this.id){
this.style.display="block";
}
else{
this.style.display="none";
}
});

}

最佳答案

您需要使用 URL 的 GET parameters :

lunch.html?item=2

结合将变量传递到 JavaScript 函数中:

// Set up an object for GET parameter
var $_GET = {};

// Find and extract the various GET parameters
if(document.location.toString().indexOf('?') !== -1) {
var query = document.location.toString().replace(/^.*?\?/, '').replace(/#.*$/, '').split('&');
for(var i=0, l=query.length; i<l; i++) {
var aux = decodeURIComponent(query[i]).split('=');
$_GET[aux[0]] = aux[1];
}
}

// Target a specific get parameter, given the GET parameter name
var tabNum = $_GET['item']; // Comes through as '2' in this example

// Pass the parameter into the function
function tabulate(tabNum){
$('.recipe').each(function() {
if(tabNum==this.id){
this.style.display="block";
}
else{
this.style.display="none";
}
});
}

参见this post this post 以供进一步引用。

希望这有帮助! :)

关于javascript - 将变量传递给另一个页面上的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44531763/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com