Rumah > Soal Jawab > teks badan
我想实现,先获取链接的id,然后通过id获取链接的href打开网页,下面的代码不可行。直接获取href是可以的,求解。
<html>
<head>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">
</script>
<script type="text/javascript">
function openMusic() {
var urlid = $('a').attr('id');
var urlhref = $('urlid').attr('href');
window.open(urlhref, "", "toolbar=1,location=1,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,top=200,left=700,width=610,height=290");
}
</script>
</head>
<body>
<a id="song1" href="http://www.songtaste.com/song/345207/" onclick="openMusic(); return false;">1.Stan - Eminem</a>
</body>
</html>
PHPz2017-04-10 13:11:38
$('#'+urlid).attr('href');
注意变量和字符串字面值的写法区别。
为什么不这样:
<!DOCTYPE html>
<meta charset="utf-8" />
<title>示例</title>
<p id="songlist">
<a href="http://www.songtaste.com/song/345207/">1.Stan - Eminem</a>
</p><script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$('#songlist > a').click(function(){
window.open(this.href, "", "toolbar=1,location=1,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,top=200,left=700,width=610,height=290");
return false;
});
</script>
为什么代码块在提交之后有一些换行被移动到底部了…………