这是我到目前为止所做的:
<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函数。我想要在新窗口中打开链接。这会被视为一种功能吗?
答案 0 :(得分:0)
语法类似于
$('#link1').click(function(){
//any misc code
//window.open('URL','name of url'); <-- to open a new window/tab
});
编辑 - 遗失的
答案 1 :(得分: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>
答案 2 :(得分: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>
答案 3 :(得分:-1)
您需要做的就是更改您的链接以获得目标...所以只需将其更改为:
<a id="link1" href="http://boston.redsox.mlb.com/index.jsp?c_id=bos&sv=1" target="redsox">
不需要javascript。