[英]How to insert every value of an array into a js-function when the page load?
[英]How to start js-function with arguments in HTML-page
我想简化下面HTML页面中的javascript。 如何使用2个参数启动FrameUpdate函数? 这样,我不必为HTML页面上的每个HREF操作重复脚本。
谢谢,舍尔德
<html>
<A HREF="geenscript.html" onclick="FrameUpdate3005(); return false;">Update 3005</A>
<p>
<A HREF="geenscript.html" onclick="FrameUpdate3017(); return false;">Update 3017</A>
<SCRIPT TYPE="text/javascript" LANGUAGE="JavaScript">
<!--
function FrameUpdate3005() {
parent.boven.location.href = "3005-n.htm";
parent.onder.location.href = "3005-t.htm";
}
//-->
</SCRIPT>
<SCRIPT TYPE="text/javascript" LANGUAGE="JavaScript">
<!--
function FrameUpdate3017() {
parent.boven.location.href = "3017-n.htm";
parent.onder.location.href = "3017-t.htm";
}
//-->
</SCRIPT>
</html>
这样的事情会做的工作:
<a href="geenscript.html" onclick="FrameUpdate(3005); return false;">Update 3005</a>
<a href="geenscript.html" onclick="FrameUpdate(3017); return false;">Update 3017</a>
<script type="text/javascript">
function FrameUpdate(value) {
parent.boven.location.href = `${value}-n.htm`;
parent.onder.location.href = `${value}-t.htm`;
}
</script>
只需将FrameUpdate3005()更改为:
<A HREF="geenscript.html" onclick="FrameUpdate3005(param1Value,param2Value); return false;">Update 3005</A>
和
<SCRIPT TYPE="text/javascript" LANGUAGE="JavaScript">
<!--
function FrameUpdate3005(param1,param2) {
// logic that uses param1 and param2
parent.boven.location.href = "3005-n.htm";
parent.onder.location.href = "3005-t.htm";
}
//-->
</SCRIPT>
为什么不尝试简单地使它成为一个函数,而不是每个函数都有一个函数? 他们似乎想要相同的结果:
function frameUpdate( id ) { parent.boven.location.href = id + "-n.htm"; parent.onder.location.href = id + "-t.htm"; // Since you always want to return false, why not wrap it in here? // No need to add a separate `return false`. return false; }
<a href="geenscript.html" onclick="return FrameUpdate( 3005 );">Update 3005</a> <a href="geenscript.html" onclick="return FrameUpdate( 3017 );">Update 3017</a>
不过,有几点注意事项,您至少缺少<body>
标记,并且<head>
也应该位于其中。 并且您的<p>
标记将打开并且永不关闭。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.