仍然没有添加到 DOM 中,但如果插入了警报/确认,则会获取标签 ID
Still not added to the DOM but if and alert/confirm inserted get the tag ID
我有这段jquery/javascript,我不明白为什么会发生这种情况以及我应该如何修复它。 带有 id="group_"+num 的标签加载了 ajax,所以它似乎还没有被添加到 DOM 中,有趣的是如果我在中间放一个警报,函数就可以工作,没有那个警报什么都没有发生,¿任何人都可以解释那里有什么警报以及我应该怎么做才能在 DOM 中获取该 id,以便 jquery 可以将长度计算为 1?当然,如果我得到任何其他未加载 ajax 的 id,则可以工作。
$(document).ready(function(){
$(function() {
var hash = window.location.hash;
var num = hash.replace("#subcat_group_","");
$('[id*="subcat_group_"]').hide();
// here the "id" loaded with ajax seems still not loaded.
var len = $("#group_"+num).length;
//Here variable "len" still 0
/***** with this alert works , just an alert anything inside
alert(num);
with this alert works *****/
/*******if i use "len" here still 0 even if alert is before *******/
if( $("#group_"+num).length === 1){
/******if i get again for id lenght with alert before , then is 1 ******/
if( $("#group_"+num).length === 1){
var group_name = $("#group_"+num).attr("title");
swapContentV1(num,group_name);
}
$("#subcat_group_"+num).show();
});
});
谢谢。
警报会强制脚本等待...以便您的响应并完成 Ajax 调用。没有警觉,你看起来"太早了"——那时阿贾克斯还没有完成。
你应该把你的代码放到成功函数中,正如shoogle在他的评论中建议的那样。
相关文章:
- 如何将HTML id分配给元素,以及如何将JavaScript应用于元素
- difference between '$(<%= DDL.ID %>) & $('
- 使用Javascript获取所选选项ID
- 根据id将json数组组合为一个json数组
- 是否有一个JS/jQuery函数可以获取某个类的每个元素的ID
- 对id以某个字符串开头的元素进行计数
- 如果元素's的ID以数字开头
- 通过id和class属性获取元素
- 根据选项卡内部的文本链接中的哈希ID动态选择jqueryUI选项卡
- 使用当前日期生成随机id
- Javascript-ID冲突的几率
- Href:当前DIV中的目标ID
- 无法获取属性'Id'使用Knockout.js的未定义或空引用API
- 按钮字段确认点击不'不起作用
- Javascript复选框函数:;缺少:在属性id之后"
- Twitter引导程序Typeahead-Id&标签
- 仍然没有添加到 DOM 中,但如果插入了警报/确认,则会获取标签 ID
- Dojo确认对话框'缺少:在属性id'之后;和'找不到节点'
- 如何在警报确认删除中获得id
- 角,Alertify:多个确认$index id's