gpt4 book ai didi

javascript - 如何使用jquery获取div内链接的href?

转载 作者:行者123 更新时间:2023-12-03 01:50:42 26 4
gpt4 key购买 nike

请帮忙,我尝试使用 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'));
});
});
});

关于javascript - 如何使用jquery获取div内链接的href?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50424764/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com