我在从超文本标记语言jQuery中选择一个元素时遇到了问题。这可能是因为我试图访问的元素是通过getJson动态安装的,但当然是在执行下面的js之前。问题是什么?如何解决?
<table id="myTable">
<tbody>
<!-- inserted with getJson -->
<tr>
<td> <input type='checkbox' id="myInput0"> </td>
<td class="myClass">some text</td>
</tr>
<!-- other rows -->
</tbody>
</table>
var id = "myInput" + 0;
var text = $("#"+id).closest('tr').find('td.myClass').text();
alert(text)
发布于 2020-04-13 02:52:11
我尝试使用getJSON并动态插入元素来重新创建该场景,然后您可以执行以下操作:
$( document ).ready(function() {
const url = 'https://jsonplaceholder.typicode.com/todos/1';
$.getJSON( url, function( data ) {
$('#main').append(data.title);
});
});
希望它能有所帮助:)
发布于 2020-04-13 02:49:08
看看这个Stack Overflow Q&A - How do I attach events to dynamic HTML elements with jQuery? --你可以采取几种方法,但最有趣的是,你需要绑定到元素的主体或容器,而不是元素本身。
欢迎来到user21社区!希望这些信息能有所帮助。
https://stackoverflow.com/questions/61176428
复制相似问题