![](/img/trans.png)
[英]How to get href url if href contains in div tag using jquery
[英]How to get href of links inside div using jquery?
请帮忙,我尝试使用jquery获取div内链接的href,但不幸的是,它不起作用。
这是我的页面,在这里我动态地在#films中获取其他div(位于其中的链接)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Cinemas</title>
</head>
<body>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<div id = "films"></div>
<script src="script.js"></script>
</body>
</html>
这是我的script.js:
$(document).ready(function() {
$.get(myUrl, function(data) { // this block works
$('#films').html($('.block_afisha', data).html())
});
$('#films').find('a').each(function() { // there is no iterations here
console.log($(this).attr('href'));
});
});
我该如何解决?
您可以尝试以下代码-
$(document).ready(function() {
$.get(myUrl, function(data) { // this block works - async block
$('#films').html($('.block_afisha', data).html());
$('#films').find('a').each(function() { // this will work if above call has added links with anchor tag
console.log($(this).attr('href'));
});
});
});
您可以只加载链接,然后遍历它们:
<div id="elems">
<a href="#one">One</a>
<a href="#two">Two</a>
<a href="#three">Three</a>
</div>
$.each($('#elems > a'),function(data,value){
console.log($(value).attr('href'));
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.