Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/427.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在单击链接时获取要运行的函数_Javascript_Html - Fatal编程技术网

Javascript 在单击链接时获取要运行的函数

Javascript 在单击链接时获取要运行的函数,javascript,html,Javascript,Html,以下是我到目前为止的情况: <html> <head> <title>Major League Baseball</title> </head> <body> <script type="text/javascript" language="JavaScript"> document.writeln( "2013 World Series" ); var today= new Date()

以下是我到目前为止的情况:

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

美国职业棒球大联盟
书面文件(“2013年世界系列”);
var today=新日期()
$(文档).ready(函数(){
});
函数changeLang(lang){
document.cookie='myCulture='+lang;
window.location.reload();
返回false;
}
link1.onclick=function(e){returnmyhandler(e);};
我试图在单击链接时添加一个javascript函数。我想在新窗口中打开链接。这算是函数吗?

语法如下

$('#link1').click(function(){
        //any misc code
        //window.open('URL','name of url'); <-- to open a new window/tab
    });
$('#link1')。单击(函数(){
//任何杂项代码

//window.open('URL','name of URL');您需要做的就是更改您的链接以获得目标…所以只需将其更改为:

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

不需要javascript。

我会更改

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


当点击这个链接时,我会用它来执行一个函数

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

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

函数myHandler(文本){
警报(文本);
}
$(文档).on('click','a#link1',function(){
myHandler($(this.attr('href'));
});
编辑:当您使用此选项时,应能正常工作:

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

美国职业棒球大联盟
函数myHandler(文本){
警报(文本);
}
$(文档).on('click','a#link1',function(){
myHandler($(this.attr('href'));
});

阅读您的评论后,我认为以下内容将满足您的需要。(无Jquery,纯javascript)


函数someFunction(){
警报(“您在链接上调用了函数单击”);
}

这个链接可能是比较简单的,这取决于你到底想做什么工作。你认为这是一个JavaScript函数吗?看起来你的“到目前为止”是一组不相关的代码粘贴在一起。不,我不会考虑<代码>目标=“空白”。
一个javascript函数-它是html/DOM的一部分。它是
标记的一个属性。感谢您的帮助。作为示例使用的函数应该做什么?我将它添加到文档中,它似乎没有做任何特殊的事。myHandler是一个示例,不存在函数。您可以为任何内容定义函数在('click',a#link1',function(){
行。
function myHandler(){alert('hello');}
会在您单击链接时发出警报。function myHandler(){alert('hello');}$(文档)。on('click',a#link1',function(){myHandler()});这是我的文档上的外观,但当我单击链接时,警报不会弹出。我做错了什么?在我的答案中添加了一个函数。复制/粘贴此函数,它会提醒链接的href。有关JQuery的更多函数,请参阅。您能更具体一点吗?您会编写什么而不是编写“someFunction”如果您注意到在script标记中,我有一个名为
someFunction()
的函数,只要onclick引用了您声明它将工作的函数。使用您的示例。单击链接时不会发生任何事情。
<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>
<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>