简体   繁体   English

创建元素后如何选择元素?

[英]How to select an element right after its creation?

I have a function which shoold create an element and save it's measurements into an array. 我有一个函数应该创建一个元素并将其度量保存到数组中。 But it doesn't work because my just created element .width() returns NULL . 但这不起作用,因为我刚刚创建的元素.width()返回NULL And another part of my function, which shoold check if DOM exists, doesn't work either because of the same problem: it seems like the JavaScript engine just cannot find a DOM, I just created, by it's ID. 我的函数的另一部分(应检查DOM是否存在)也由于相同的问题而无法正常工作:似乎JavaScript引擎无法通过ID来找到我刚刚创建的DOM。 Where is the error and how can I select an element right after its creation? 错误在哪里,如何在创建元素后立即选择元素?

Here is the code: 这是代码:

var objCounter = 1;

function objectCreate(type) {
// get ID, check if DOM exists

objectID = 'object-' + objCounter;

if ($(objectID).length) {
    objCounter += 1;
}

// get object type and create

var types = {
    'label' : function () {
        $("<div id=" + objectID + "><span>Hello world</span></div>").addClass('obj obj-label').appendTo('#page').draggable({
            revert : function(valid) {
                if(!valid) {
                    this.remove();
                }
            }
        });
        params = {
            w : $(objectID).width($(objectID > "span").width() * 1.25),
            h : $(objectID).height($(objectID > "span").width() * 1.25)
        };
        console.log($(objectID).width()); // --> NULL
    },
    /*...*/
};

return types[type]();

$(document).ready(function(){

$('#label').mousedown(function(){
    objectCreate('label');
  });
});

In your case, objectID is just the id itself, the selector requires # aswell. 在您的情况下, objectID只是ID本身,选择器还需要# So change 所以改变

console.log($(objectID).width()); // --> NULL

to

console.log($('#' + objectID).width()); // --> MAGIC

Futhermore, this is invalid syntax: 此外,这是无效的语法:

$(objectID > "span")

Correct that to 更正为

$('#' + objectID + " > span")

You might want to take a look at string concatenation in js 您可能想看看js中的字符串连接

Actually, you can save the jQuery element itself in a variable directly when creating it: 实际上,您可以在创建jQuery元素时直接将其本身保存在变量中:

var myobject = $("<div id=" + objectID + "><span>Hello world</span></div>");

myobject.addClass('obj obj-label').appendTo('#page').draggable()...
...
myobject.width();

Your current code doesn't give you the value because the objectID variable doesn't contain the preceding # for the id selector to work: 您的当前代码未提供该值,因为objectID变量不包含前面的#以使ID选择器正常工作:

params = {
    w: $('#' + objectID).width($('#' + objectID + " > span").width() * 1.25),
    h: $('#' + objectID).height($('#' + objectID + " > span").width() * 1.25)
};

That being said, you should save the jQuery object containing the new element to a variable to save querying the DOM again: 话虽如此,您应该将包含新元素的jQuery对象保存到变量中,以再次保存对DOM的查询:

var $object = $("<div id=" + objectID + "><span>Hello world</span></div>").addClass('obj obj-label').appendTo('#page').draggable({
    revert : function(valid) {
        if(!valid) {
            this.remove();
        }
    }
});
params = {
    w: $object.width($object.find("> span").width() * 1.25),
    h: $object.height($object.find("> span").width() * 1.25)
};

Also note the > selector needs correct concatenation. 还要注意>选择器需要正确的串联。

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

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