在$ .getJSON成功函数中,我首先触发另一个元素的click事件:
$('#' + data[0].ID).trigger('click');
Run Code Online (Sandbox Code Playgroud)
触发的click事件有自己的$ .getJSON方法将一堆数据加载到div中.触发事件后的下一行:
$.each(data[0].ListEntries, function (key, val) {
//this relies on the triggered click event
//having completely loaded all data!
}
Run Code Online (Sandbox Code Playgroud)
起初$ .each似乎没有做任何事情,但我在触发事件后立即添加了警报.响应警报后,$ .each中的代码显示了它应该是什么.
我猜测$ .each在点击事件完成加载数据之前正在运行.
setTimeout暂停足够长的时间以使click事件加载数据,但我宁愿不设置任意时间:
setTimeout(function() {
$.each(data[0].ListEntries, function (key, val) {
//this relies on the triggered click event
//having completely loaded all data!
}
}, 1000);
Run Code Online (Sandbox Code Playgroud)
我也试过$ .when和$ .then无济于事(尽管在$ .each里面添加警报,然后在$.然后为$ .each工作创建延迟):
$.when($('#' + data[0].ID).trigger('click')).then(function () {
$.each(data[0].ListEntries, function (key, val) {
//this relies on the triggered click event
//having completely loaded all data! …Run Code Online (Sandbox Code Playgroud) 我使用这篇文章来创建一个Image帮助器:http: //www.asp.net/mvc/tutorials/older-versions/views/using-the-tagbuilder-class-to-build-html-helpers-cs.它说它取代了时期,但没有提到其他限制.
除非我尝试使用GUID字符串作为id,否则一切正常.
剃刀代码:
@Html.Image(id, ....) //passes a GUID string to helper fine
Run Code Online (Sandbox Code Playgroud)
ImageHelper与网页描述完全一样:
builder.GenerateId(id);
//breakpoint after this shows the builder object
//does not create an id attribute when id is a GUID string.
//using id.ToString() doesn't work for GUIDs either
Run Code Online (Sandbox Code Playgroud)
在MergeAttributes中使用GUID字符串可以正常工作:
builder.MergeAttribute("title", id); //the GUID string is img title no problem
builder.MergeAttribute("class", id); //the GUID string is class as expected
Run Code Online (Sandbox Code Playgroud)
使用GUID字符串作为ID时是否存在某些限制或解决方法?