使用方法 onclick () 触发一个函数,该函数打开它从单击按钮抓取的窗口

using the method onclick () to trigger a function that opens a window it grabs from the clicked button

本文关键字:函数 单击 抓取 窗口 按钮 onclick 使用方法 一个      更新时间:2023-09-26
    <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 文本,同时允许它通过方法 call onsubmit 调用 goThere((。

最后,它

应该只获取 url,确保它以 http://开头(在这种情况下没关系,因为用户没有输入 url,但我想稍后将其保留用于其他目的(并在带有菜单栏和可调整大小属性的新窗口中打开它。

对不起,长帖子。任何帮助将不胜感激。

goThere呼叫中传入this。这会将点击的元素引入您的 goThere 函数。然后,访问单击按钮的属性。

http://jsfiddle.net/wJMgb/

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;
}