简体   繁体   中英

How to mimic an HTML element and add attributes to it in Javascript/jQuery

I have a method called handle_value_tag_click(value) which takes an HTML element (value) as a parameter.

I want to call this function from another function, but this time I don't have a 'ready made' HTML element ... in the initial situation I pass a this element and the function unwraps the parameters appropriately.

My guess is I have to construct the element by using a JS object. This is my attempt:

    var value = new Object();
        value.attr("valueType","NumericQueryValue");
        value.attr("lower",lowerBound);
        value.attr("upper",upperBound);
    handle_value_tag_click(value);

However I get the error value.attr is not a function , how can I solve this error, or get the appropriate behavour (passing parameters to the handle_value_tag_click() function) in some other way.

如果你这样说呢?:

var value = $("<div />");

Why don't you create an element like this:

var value = $(document.createElement("div"));

It won't be in the DOM but every jQuery method will work on it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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