<script language="JavaScript">
function goThere()
{
var the_url = window.document.form.button.value;
var good_url = fixURL(the_url);
var new_window = window.open(good_url,"new_window","menubar,resizeable");
}
function fixURL(the_url)
{
var the_first_seven = the_url.substring(0,7);
the_first_seven = the_first_seven.toLowerCase();
if (the_first_seven != 'http://')
{
the_url = "http://" + the_url;
}
return the_url;
}
</script>
</head>
<body>
<form name="the_form" onclick="goThere()"; return false;">
<input type="button" name="the_url" class="broadGroups" onClick="goThere()" value="http://en.wikipedia.org/wiki/Category:Sports"></input>
<input type="button" name="the_url" class="broadGroups" onclick="goThere()" value="http://en.wikipedia.org/wiki/Category:Film"></input>
</form>
</body>
</html>
所以这段代码可能完全搞砸了,但这就是我要做的事情。
标签内有两个按钮。我希望每个人都使用onsubmit方法来触发goThere()函数。如何设置它以便将the_url设置为我从按钮标记中提取的值。我还希望能够在按钮上放置非url文本,同时允许它通过方法调用onsubmit调用goThere()。
最后它应该只是取url,确保它以http://开头(在这种情况下它并不重要,因为用户没有输入url,但我想保留它用于其他以后的目的)并在一个带有菜单栏和可调整大小的属性的新窗口中打开它。
对不起,很长的帖子。任何帮助将不胜感激。
答案 0 :(得分:1)
在this
来电中传递goThere
。这会将单击的元素引入goThere函数。然后,您可以访问所单击按钮的属性。
onClick="goThere(this)"
function goThere(elem) {
var the_url = elem.value;
var good_url = fixURL(the_url);
var new_window = window.open(good_url, "new_window", "menubar,resizeable");
}
function fixURL(the_url) {
var the_first_seven = the_url.substring(0, 7);
the_first_seven = the_first_seven.toLowerCase();
if (the_first_seven != 'http://') {
the_url = "http://" + the_url;
}
return the_url;
}