无法使用jQuery(JavaScript)附加html

无法使用jQuery(JavaScript)附加html,javascript,jquery,html,dom,append,Javascript,Jquery,Html,Dom,Append,在此jquery插件中: 有这样一行: $(obj).append('<div id="textholder'+randID+'" class="textholder" style="position:absolute;bottom:0px;margin-bottom:'+-imgHeight*o.textholderHeight+'px;left:'+$(obj).css('paddingLeft')+'"></div>'); 但是,我的上述附

在此jquery插件中:

有这样一行:

            $(obj).append('<div id="textholder'+randID+'" class="textholder" style="position:absolute;bottom:0px;margin-bottom:'+-imgHeight*o.textholderHeight+'px;left:'+$(obj).css('paddingLeft')+'"></div>');
但是,我的上述附加功能不起作用。嵌套的div永远不会出现,因此当我稍后执行此操作时:

                    if(t != null)
                {
                    $('#textholder'+randID+' div').html(t).animate({marginBottom:'0px'},500); // Raise textholder
                    showminmax();
                }
由于从未创建嵌套的div,因此文本不可见

所以我非常困惑。如果你看一下原始插件,这一行是有效的:

 $('#textholder'+randID+' div').html(t)
当我在创建了正确的div之后立即将其附加到它,但它并不存在,正如你们所建议的那样,它怎么能够在这里定位正确的div呢

这也不起作用:

                var $texthold = jQuery('<div id="textholder'+randID+'" class="textholder" style="position:absolute;bottom:0px;margin-bottom:'+-imgHeight*o.textholderHeight+'px;left:'+$(obj).css('paddingLeft')+'"></div>');
            $(obj).append($texthold);
            $texthold.append('<div></div>')

感谢您的回复。

听起来好像randID在$obj.append。。。行和您添加的$'textholder'+randID。。。线您确定这两行上的randID值相同吗?

您确定$obj确实指的是要附加到的元素吗?为了以防万一,您可能想反过来尝试:

$('<div id="textholder'+randID+'" class="textholder" style="position:absolute;' 
    +'bottom:0px;margin-bottom:'+(-imgHeight*o.textholderHeight)
    +'px;left:'+$(obj).css('paddingLeft')+'"></div>'
).appendTo(obj);
尝试:


文档是。

这似乎有效:

            function showtext(t)
            {

                if(t != null)
                {   

                    if(typeof $('#textholder'+randID).find('div')[0] == 'undefined'){
                        $('#textholder'+randID).append('<div style="display:table-cell; height: 94.25px; vertical-align:bottom;"></div>');
                    }

                    $('#textholder'+randID+' div').html(t);

                    $('#textholder'+randID).animate({marginBottom:'0px'},500); // Raise textholder
                    showminmax();

                }
            }

我不得不使用typeof检查div是否被声明,这有点疯狂。我不明白为什么插件的作者决定随机分配整数作为div的id。通过这样做,javascript似乎很难保留随机创建的id。这是我能想到的异常行为的唯一解释。

height:94.25px真的吗?94和四分之一像素-在尝试附加到它之前,您确定它确实存在吗?你在Firebug中验证过吗?您可以使用jsFiddle.net创建复制吗?这是jQuery擅长的标准DOM操作代码。所以我很惊讶它不起作用。@crowder,你知道吗,测量值为33.33px的diff 3 Div加起来可以达到99px或100px,这取决于浏览器?不久前我遇到了这个问题,结果存储了十进制余数并将其添加到最后一个余数中以保持高度一致。@Matrym:或者,你知道,使用33.33、33.33和33.34.:-只是像素的分数在HTML级别没有任何意义。“如果要处理带有亚像素渲染的平板显示器,它们可以在显示器驱动程序级别上实现。”Crowder理解并同意每像素小数是无意义的,但我只是分享一个有趣的发现。有些浏览器无法达到33.9999px。如果你想将12个月作为div放入一个100px高度的容器中,这可能会导致问题,因此你需要存储十进制数并将其带到下一个容器中。我只是想说,如果randID在自己的代码中添加第二个div,看起来连定义都没有,而不是在pluginI中用控制台输出更新问题。谢谢你的回复。
$('<div id="textholder'+randID+'" class="textholder" style="position:absolute;' 
    +'bottom:0px;margin-bottom:'+(-imgHeight*o.textholderHeight)
    +'px;left:'+$(obj).css('paddingLeft')+'"></div>'
).appendTo(obj);
$(obj).after('html');
            function showtext(t)
            {

                if(t != null)
                {   

                    if(typeof $('#textholder'+randID).find('div')[0] == 'undefined'){
                        $('#textholder'+randID).append('<div style="display:table-cell; height: 94.25px; vertical-align:bottom;"></div>');
                    }

                    $('#textholder'+randID+' div').html(t);

                    $('#textholder'+randID).animate({marginBottom:'0px'},500); // Raise textholder
                    showminmax();

                }
            }