Javascript 找不到由Ajax动态创建的元素
在我的jsp页面中,我使用Ajax调用将一个表体插入到现有表中。我有另一个jquery函数,它在表中的某些行中查找最大值并更改背景颜色。但是,jquery似乎找不到由Ajax动态创建的元素 Ajax调用:Javascript 找不到由Ajax动态创建的元素,javascript,jquery,html,ajax,jsp,Javascript,Jquery,Html,Ajax,Jsp,在我的jsp页面中,我使用Ajax调用将一个表体插入到现有表中。我有另一个jquery函数,它在表中的某些行中查找最大值并更改背景颜色。但是,jquery似乎找不到由Ajax动态创建的元素 Ajax调用: function getRelTable(type) { $.ajax({ type : "POST", dataType : "html", cache : false, url : _ctx + "/monitor/g
function getRelTable(type) {
$.ajax({
type : "POST",
dataType : "html",
cache : false,
url : _ctx + "/monitor/get_table.do",
data : {
"type" : type,
},
success : function(content) {
//var t_body = $(content);
$("#table_body").html(content);
findMax();
},
error : function() {
ui.msg('信息加载错误', 0);
}
});
}
此Ajax中的内容来自jsp页面:
<c:forEach var="nodeRel" items="${nodeRelInfo}" varStatus="stc">
<c:choose>
<c:when test="${stc.first}">
<tr>
<td style="vertical-align: middle; text-align: center;"
rowspan="3">${stc.count}</td>
<td style="vertical-align: middle; text-align: center;"
valign="middle" align="center" rowspan="3">${nodeRel.fromNodeName}
--> ${nodeRel.toNodeName}</td>
<td></td>
<c:forEach var="x" begin="0" end="23" step="1">
<td>${x}</td>
</c:forEach>
</tr>
<tr class="value_row">
<td>${nodeRel.fromNodeName}发送</td>
<c:forEach var="node" items="${nodeRel.sendFinalList}">
<td class="value_cell" value="${node}"><c:choose>
<c:when test="${empty node}">
-
</c:when>
<c:otherwise>
${node}
</c:otherwise>
</c:choose></td>
</c:forEach>
</tr>
<tr class="value_row">
<td>${nodeRel.toNodeName}发送</td>
<c:forEach var="node" items="${nodeRel.recvFinalList}">
<td class="value_cell" value="${node}"><c:choose>
<c:when test="${empty node}">
-
</c:when>
<c:otherwise>
${node}
</c:otherwise>
</c:choose></td>
</c:forEach>
</tr>
</c:when>
<c:otherwise>
<tr class="value_row">
<td style="vertical-align: middle; text-align: center;"
rowspan="2">${stc.count}</td>
<td style="vertical-align: middle; text-align: center;"
valign="middle" align="center" rowspan="2">${nodeRel.fromNodeName}
--> ${nodeRel.toNodeName}</td>
<td>${nodeRel.fromNodeName}发送</td>
<%-- <c:forEach var="node" items="${nodeRel.sendFinalList}">
<td class="value_cell" value="${node}"><c:choose>
<c:when test="${empty node}">
-
</c:when>
<c:otherwise>
${node}
</c:otherwise>
</c:choose></td>
</c:forEach> --%>
<c:forEach var="x" begin="0" end="23" step="1">
<td value="${x}">${x}</td>
</c:forEach>
</tr>
<tr class="value_row">
<td>${nodeRel.toNodeName}发送</td>
<c:forEach var="node" items="${nodeRel.recvFinalList}">
<td class="value_cell" value="${node}"><c:choose>
<c:when test="${empty node}">
-
</c:when>
<c:otherwise>
${node}
</c:otherwise>
</c:choose></td>
</c:forEach>
</tr>
</c:otherwise>
</c:choose>
</c:forEach>
我的代码怎么了
更新:
答案表明我无法选择在成功回调函数中创建的元素。那么,有没有其他方法可以在动态创建的内容中找到最大值并操作此内容?您可以创建一个演示来显示问题吗?我想这与问题相同。看看答案。这个答案是否意味着无法访问成功回调中创建的元素@Jakub JečmínekTry移动findMax();在success callback之外,将其放入i done()callback()并查看其行为。您可以使用实际id(“#table_body”)进行尝试吗而不是findMax方法中的这个?你能创建一个演示来显示这个问题吗?我想这和问题是一样的。看看答案。这个答案是否意味着无法访问在成功回调中创建的元素?@Jakub JečmínekTry将你的findMax()移到成功回调之外,并将它放在I done()回调()然后看看它是如何工作的。你能用实际id(“#table_body”)代替findMax方法中的这个吗?
function findMax() {
$('tr').each(function(){
//get the first value
var max = $(this).find('td.value_cell').first().attr("value");
if(isNaN(parseInt(max)))
return;
var highestElement = $(this).find('td.value_cell').first();
$(this).find('td.value_cell').each(function(){
var current = $(this).attr("value");
if(!isNaN(parseInt(current))){
if(current >= max) {
max = current;
highestElement = $(this);
}
}
});
if(max != 0)
highestElement.css('background-color', 'red');
});
}