简体   繁体   English

jQuery Colorbox未来元素追加

[英]JQuery Colorbox Future Element Append

I have an html that has a link: 我有一个带有链接的html:

<a id="myLink" href="/aa.html" title="New Bangoo" class="bangoo">Bangoo</a>

and I include a js file that has this lines: 并且我包含一个包含以下内容的js文件:

$("#myLink").live('click', function() { 
var typi = 'Ginga';       
    $("#typeList").append('<option>' + typi + '</option>');        
});

aa.html has that: aa.html具有:

<label for="typeList">Type</label>
<div>
   <select id="typeList"></select>
</div>

aa.html opens on a colorbox. aa.html在颜色aa.html打开。 Everything is OK but I can not append types to select list. 一切都很好,但我不能将类型附加到选择列表中。 It has no elements when it finishes to open. 当它完成打开时,它没有任何元素。 It used to work but now not working. 它曾经可以工作,但现在不起作用。 It may be because of js file tries to append to list before html loads? 可能是因为js文件试图在html加载之前追加到列表?

Any ideas? 有任何想法吗?

EDIT: 编辑:

When I do that it works: 当我这样做时,它会起作用:

$("#myLink").live('click', function() { 
alert('Wait Here!');
var typi = 'Ginga';       
    $("#typeList").append('<option>' + typi + '</option>');        
});

When I alert anything it waits and at this time it has enough time to append to select list. 当我发出警报时,它会等待,这时它有足够的时间来追加选择列表。 How can I sure that external link has loaded so I can start to append? 我如何确定外部链接已加载,以便可以开始追加?

PS: My problem is that I am trying to append an HTML element that I load it after I click myLink . PS:我的问题是我试图添加一个HTML元素,然后单击myLink加载它。 aa.html doesn't place at dom at the beginning, I load it from an external source. aa.html在一开始并不放在dom上,而是从外部源加载它。

After external page loads at screen it has no elements at select list. 在屏幕上加载外部页面后,在选择列表中没有任何元素。 When I try to run same code from Firebug it adds options to select list and works. 当我尝试从Firebug运行相同的代码时,它会添加选项以选择列表并起作用。 It means that code works but there is a timing and asynchronism problem that I want to append elements to a select list before it locates at dom tree. 这意味着代码可以工作,但是存在一个计时和异步问题,我想在元素位于dom树之前将其追加到选择列表中。 I should wait loading it and after that run my code. 我应该等待加载它,然后运行我的代码。

try this
$("#myLink").live('click', function(e) {
e.preventDefault(); 
var typi = 'Ginga';       
    $("#typeList").append($('<option></option>').val(typi ).html(typi )) ;   

});

I solved my problem. 我解决了我的问题。 colorbox plugin has a function onComplete so I used that function to be sure page loaded. colorbox插件具有onComplete函数,因此我使用该函数来确保页面已加载。

try 尝试

$("#myLink").live('click', function(e) { 
e.preventDefault();
var typi = 'Ginga';       
    $("<option/>",{text:typi,value:typi}).appendTo("#typeList");        
});

DEMO 演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM