简体   繁体   English

获取jquery创建的div的ID

[英]Get ID of div created by jquery

I have two functions, where the first creates a div in the dom and the second gets the ID via getElementById() .我有两个函数,第一个在 dom 中创建一个 div,第二个通过getElementById()获取 ID。 Unfortunately, my second function is just returning null.不幸的是,我的第二个 function 只是返回 null。

I looked in using $.Deffered() in either of my functions, but alas nothing I do seems to work我查看了在我的任何一个函数中使用$.Deffered() ,但是我所做的似乎没有任何工作

function one() {
    var dynamicDiv = $("<div>").attr("id", 'test');
    dynamicDiv.iziModal({
        //modal content here
    })
}

function two() {
    var container = document.getElementById('test');
    console.log(container.innerHTML); //not returning any data

}


enter code here

I had hoped to use this with a library that replaces images with a canvas object, but it requires that the "parent element to be a valid DOM Element".我曾希望将它与一个用 canvas object 替换图像的库一起使用,但它要求“父元素是有效的 DOM 元素”。 I have been searching for a while now, but I can not seem to find anything on the subject that is applicable.我已经搜索了一段时间,但我似乎无法找到任何适用的主题。

** Edit** Thank you for pointing out the error in my id (I had tried something else before I pasted). ** 编辑** 感谢您指出我的 id 中的错误(我在粘贴之前尝试了其他方法)。 The iziModal function ( http://izimodal.marcelodolza.com/ ) is currently pulling the content in via ajax (which I assume is the problem here): iziModal function ( http://izimodal.marcelodolza.com/ ) 目前正在通过 ajax 提取内容(我假设这是问题所在)

The iziModal Function: iziModal Function:

dynamicDiv.iziModal({
        title: '',
        autoOpen: 1,
        width: 500,
        onOpening: function (offer) {
            offer.startLoading();
            $.get(demoUrl, function (data) {
                $('#' + id + " .iziModal-content").html(data);
                offer.stopLoading();
            });
        }
    });

Content of the other file:另一个文件的内容:

<div id="test">
    <img src="./bundle/images/coupon.jpg" />
</div>
function one() {
    var dynamicDiv = $("<div>").attr("id", 'test');
    dynamicDiv.iziModal({
        //modal content here
    });
    $("body").append(dynamicDiv);
}

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

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