jquery.html()是否具有“就绪"状态?或“完整"打回来? [英] does jquery.html() have a "ready" or "complete" callback?

查看:86
本文介绍了jquery.html()是否具有“就绪"状态?或“完整"打回来?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用DOM插入来生成一些数据输入对话框.我发现在某些浏览器中,尤其是移动浏览器和IE-Mac,在.html()调用之后立即执行的操作不会触发,可能是因为修改后的DOM尚未准备好.例如:

I'm using DOM insertion to generate some data entry dialogs. I'm finding that in some browsers, particularly mobile browsers and IE-Mac, manipulations immediately after the .html() call don't fire, presumably because the modified DOM is not yet ready. For example:

$.ajax({
    type: "GET",
    url: url,
    dataType: 'html',
    success: function (data, textStatus, jqXHR) {
        $("#theDialog").html(data);

        // hide address
        $("#theDialog #BillingAddress").closest("li").hide();
        ...

这在大多数情况下都有效,但是某些浏览器有时无法隐藏指示的<li>.调试一直很困难,因为进入控制台似乎允许浏览器完成DOM插入,因此.hide() 始终在逐步执行代码时可以正常工作.

This works most of the time, but certain browsers sometimes fail to hide the indicated <li>. And it's been challenging to debug, because going to console seems to allow the browser to complete the DOM insertion, and so the .hide() always works when stepping through the code.

我想得到的是$("#theDialog").on("ready").html()方法的回调.但是那些似乎不存在.

I suppose what I'm after is either a $("#theDialog").on("ready") or a callback from the .html() method. But those don't seem to exist.

我想到了

  1. 切换到.load(),但最终我将转换为JSONP,我不认为.load()支持该功能吗?
  2. .hide()和其他DOM操作放入返回数据中的脚本标签中.
  1. Switching to .load(), but eventually I'll be converting to JSONP and I don't think .load() supports that?
  2. Putting the .hide() and other DOM manipulation in a script tag within the returned data.

我怀疑#2是推荐的方法,但想先在这里进行健全性检查.有人遇到过吗?

I suspect #2 is the recommended approach but wanted to do a sanity check here first. Has anyone experienced this?

非常感谢.

推荐答案

尽管我之前的回答很奏效,但并不能解决您的所有问题.我推荐类似的东西:

Although my previous answer worked, it didn't solve all your problems. I recommend something like:

var myDiv=document.createElement( 'div' );
myDiv.innerHTML=data;
// now do all of your initializing, passing in myDiv like $( myDiv )
$("#theDialog").html(myDiv.innerHTML)
  .find( '#BillingAddress' )
  .closest("li")
  .hide();

希望这行得通!

这篇关于jquery.html()是否具有“就绪"状态?或“完整"打回来?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆