[英]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. 我有一个名为handle_value_tag_click(value)的方法,该方法将HTML元素(值)作为参数。
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. 我想从另一个函数调用这个函数,但是这次我没有一个“现成的” HTML元素……在初始情况下,我传递了一个
this
元素,并且该函数适当地解开了参数。
My guess is I have to construct the element by using a JS object. 我的猜测是我必须使用JS对象构造元素。 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. 但是我得到了错误
value.attr is not a function
,如何解决此错误,或者以其他方式获得适当的行为(将参数传递给handle_value_tag_click()函数)。
如果你这样说呢?:
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. 它不会出现在DOM中,但是每个jQuery方法都可以使用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.