我正在尝试触发.jquery的链接点击.有人知道为什么以下不起作用.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
<title>test</title>
</head>
<body>
<div>
<a id="google_link" href="http://google.com" target="_blank">click to go to google</a>
</div>
<div id="google_link_proxy">click here to do the same as the link above</div>
<script type="text/javascript">
$("#google_link_proxy").click(function(event){
$("#google_link").click();
});
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
Rom*_*kov 37
jQuery方法完全忽略href
:
$('#google_link').click(); // ignores href!
Run Code Online (Sandbox Code Playgroud)
原生DOM方法做正确的事情:
$('#google_link')[0].click();
Run Code Online (Sandbox Code Playgroud)
无论href
是URL,片段(例如#blah
)还是甚至a,这都有效javascript:
.
Wan*_*bie 21
如果您使用jQuery和本机DOM,则可以单击锚点
// insert an <a> into document and click it **natively**
// (.get(0) returns the DOM element)
$('<a id="fred99" />').attr('href', '#david').attr('target', '_blank')
.text('LINK').appendTo('body').get(0).click();
// now we've clicked, tidy up
$('#fred99').remove();
Run Code Online (Sandbox Code Playgroud)
Jef*_*ear 20
看起来您的$("google_link_proxy")
选择器已关闭.试试$("#google_link_proxy")
.
您还需要关闭观察呼叫})
.
这些是上面代码的语法错误,虽然我不认为默认情况下这些函数是在jQuery中提供的.
以下是我认为您的追求:
$("#google_link_proxy").click(function(event){
window.open($("#google_link").attr('href'),'_blank')
});
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
72182 次 |
最近记录: |