使用一个页面中的链接链接到另一个页面的不同脚本
Using links from one page to link to different scripts from another page
好吧,所以我在代码方面很基本,但我们必须为我在学校的计算机科学课制作一个网页,其中一部分包括以前的课程安排。我将把它简化为:我在顶部有一个导航栏,其中一个选项(schedules)有一个下拉菜单,指向几个子选项(过去几年)。如果你单击主选项,它会链接到一个有按钮的页面(schedules.html)。这些按钮运行一个简单的脚本,显示嵌入在按钮下面的以前课程时间表的图片[到目前为止,所有这些都是我想要的]
我找不到如何做的是将下拉选项(过去几年)链接到时间表页面,然后在加载后运行显示图片的脚本。再说一遍,这可能是因为我真的是个新手。但我有:
HTML
<li><a href="schedules.html">Class Schedules</a>
<ul>
<li><a href="?">2015</a></li>
<!--want to happen: when select a year, loads schedule page then loads the schedule for that year, -->
<li><a href="?">2014</a></li>
<li><a href="?">2013</a></li>
<li><a href="?">2012</a></li>
</ul>
</li>
在一个简单的表格中显示我设置的时间表的按钮:
<tr>
<td>2015</td>
<td><input type="button" value="Fall"onClick="pic1()"/></td>
<td><input type="button" value="Spring"onClick="pic2()"/></td>
</tr>
图片的脚本:
<script type = "text/javascript">
function pic1() /*fall 2015*/
{
document.getElementById("schedule").src = "fall15.png";
}
function pic2() /*spring 2015*/
{
document.getElementById("schedule").src ="spring15.png";
}
等等。。。
<img src = "" id = "schedule"/> <!--place where the image goes-->
如果在某个地方得到了答复,我深表歉意。我找了几天,但我不确定我要找的是什么。我没有在这里包含CSS代码,因为我不认为它会影响答案,但如果有价值的话,CSS会链接到样式表。
我不确定您真正想要的是什么,因为您的示例有两个2015年的图像,但是如果您想将它们分开,这是可行的(未经测试)。
<body onload="checkYear()">
links would be:
<a href="schedules.html?year=15&season=fall">2015 Fall</a>
<a href="schedules.html?year=15&season=spring">2015 Spring</a>
<script type="text/javascript">
function checkYear(){
var yearVal = getParameterByName('year');
var seasonVal = getParameterByName('season');
if(yearVal && seasonVal){
document.getElementById("schedule").src = (seasonVal + yearVal + '.png');
}
}
/*http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript*/
function getParameterByName(name) {
name = name.replace(/['[]/, "''[").replace(/[']]/, "'']");
var regex = new RegExp("[''?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results === null ? "" : decodeURIComponent(results[1].replace(/'+/g, " "));
}
实现这一点的方法之一是通过URL传递参数,然后在javascript文件中读取它们
通过像year=2015
这样的url或您认为合适的url传递变量,然后用location.search.split('year=')[1
]在javascript中读取它,这会为您提供所需的参数。
然后使用if-else梯形图来决定将根据年份参数显示哪些图像。
在html文件中有这个
<li><a href="schedules.html">Class Schedules</a>
<ul>
<li><a href="schedules.html?year=2015">2015</a></li>
<li><a href="schedules.html?year=2014">2014</a></li>
<li><a href="schedules.html?year=2013">2013</a></li>
<li><a href="schedules.html?year=2012">2012</a></li>
</ul>
</li>
在schedules.html文件中有这个
<tr>
<td id="year">Images</td>
<td><input type="button" value="Fall"onClick="pic1()"/></td>
<td><input type="button" value="Spring"onClick="pic2()"/></td>
</tr>
<br/><br/>
<img src = "" id = "schedule"/>
<script>
var param = location.search.split('year=')[1];
if(param == 2015){
document.getElementById("schedule").src = "https://placeholdit.imgix.net/~text?txtsize=33&txt=2015 image&w=150&h=150";
}else if(param == 2014) {
document.getElementById("schedule").src ="https://placeholdit.imgix.net/~text?txtsize=33&txt=2014 image&w=150&h=150";
}else if(param == 2013) {
document.getElementById("schedule").src ="https://placeholdit.imgix.net/~text?txtsize=33&txt=2013 image&w=150&h=150";
}else if(param == 2012) {
document.getElementById("schedule").src ="https://placeholdit.imgix.net/~text?txtsize=33&txt=2012 image&w=150&h=150";
}
function pic1() {
document.getElementById("schedule").src = "https://placeholdit.imgix.net/~text?txtsize=33&txt=fall&w=150&h=150";
}
function pic2() {
document.getElementById("schedule").src ="https://placeholdit.imgix.net/~text?txtsize=33&txt=spring&w=150&h=150";
}
</script>
信用
相关文章:
- 想要打开从链接到另一个页面的模式弹出窗口
- 将按钮链接到另一个页面的一部分
- JavaScript:将输入范围行为链接到另一个元素
- 正在验证并链接到另一个页面
- 将登录链接到另一个网站而不提供密码
- 在JointJS中将一个链接链接到另一个
- 点击jsp页面上的链接打开一个文件
- 将href链接到另一个HTML文档中的锚点并将其居中
- 我想让HTML链接打开一个js手风琴
- 使用 JavaScript 向一组链接中的第一个链接添加一个属性
- 链接到另一个带有jQuery元素的页面
- 为链接打开一个新窗口,但希望下一页仍在“主体”框架中
- 将 href 值从 标记链接到另一个 html 页面
- 链接到另一个具有过渡的页面
- 使用JavaScript和html将图像和超链接从一个位置随机移动到另一个位置
- 使用一个页面中的链接链接到另一个页面的不同脚本
- 如何在jquery中链接另一个HTML页面到海报图像
- 如何使用按钮链接另一个页面
- 转换链接到另一个链接- javascript
- 我如何使一个图像的链接,链接到另一个图像,使用javascript