0

这是我到目前为止所拥有的:

<html>
<head>
   <title>Major League Baseball</title>
</head>

<body>

<script type="text/javascript" language="JavaScript">
   document.writeln( "2013 World Series" );
   var today= new Date()
   $(document).ready(function () {
});
function changeLang(lang) {
    document.cookie = 'myCulture=' + lang;
    window.location.reload();
    return false;
}

link1.onclick = function(e) { return myHandler(e); };

</SCRIPT>
<BODY onload=alert(today)>

<a id="link1" href="http://boston.redsox.mlb.com/index.jsp?c_id=bos&sv=1">Red Sox homepage</a>

<body onload="onload();">
    <input type="text" name="enter" class="enter" value="" id="lolz"/>
    <input type="button" value="click" onclick="kk();"/>



</body>
</html>

单击链接时,我正在尝试添加 javascript 函数。我希望链接在新窗口中打开。这会被认为是一个功能吗?

4

4 回答 4

0

我会改变

<a id="link1" href="http://boston.redsox.mlb.com/index.jsp?c_id=bos&sv=1">Red Sox homepage</a>

<a id="link1" href="http://boston.redsox.mlb.com/index.jsp?c_id=bos&sv=1" target="_blank">Red Sox homepage</a>

单击此链接时,我将使用它来执行功能。

<script type="text/javascript">
    function myHandler(text) {
        alert(text);
    }

    $(document).on('click', 'a#link1', function() {
        myHandler($(this).attr('href'));
    });
</script>

编辑:当你使用这个时应该工作:

<html>
<head>
    <title>Major League Baseball</title>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js" type="text/javascript">
    <script type="text/javascript">
        function myHandler(text) {
            alert(text);
        }

        $(document).on('click', 'a#link1', function() {
            myHandler($(this).attr('href'));
        });
    </script>
</head>
<body>

    <a id="link1" href="http://boston.redsox.mlb.com/index.jsp?c_id=bos&sv=1">Red Sox homepage</a>

</body>
</html>
于 2013-10-24T21:15:19.497 回答
0

语法就像

$('#link1').click(function(){
        //any misc code
        //window.open('URL','name of url'); <-- to open a new window/tab
    });

编辑——缺失的

于 2013-10-24T21:08:59.280 回答
0

阅读您的评论后,以下内容将实现您的需求 - 我认为。(没有 Jquery,纯 javascript)

<script>
function someFunction(){
    alert("You called a function on the link click");
}
</script>
<a target="_blank" onclick="someFunction()" id="link1" href="http://boston.redsox.mlb.com/index.jsp?c_id=bos&sv=1">Red Sox homepage</a>
于 2013-10-24T21:20:53.463 回答
-1

您需要做的就是将链接更改为具有目标...所以只需将其更改为:

<a id="link1" href="http://boston.redsox.mlb.com/index.jsp?c_id=bos&sv=1" target="redsox">

不需要javascript。

于 2013-10-24T21:09:25.903 回答