javascript - Creating a DOM element with jQuery - does not implement "node"? - Stack Overflow

I'm currently implementing CodeMirror and I'm trying to use the addLineWidget function.It

I'm currently implementing CodeMirror and I'm trying to use the addLineWidget function.

It's second parameter takes a DOM Node, so I thought to construct it with jQuery like this:

var widget = $('<span class="ct-description-widget">' + descr + '</span>').get();

However, when I pass it to the function, it throws an exception:

TypeError: Value does not implement interface Node.

How can I make it a true Node? Note that I cannot append it anywhere into the DOM!

I'm currently implementing CodeMirror and I'm trying to use the addLineWidget function.

It's second parameter takes a DOM Node, so I thought to construct it with jQuery like this:

var widget = $('<span class="ct-description-widget">' + descr + '</span>').get();

However, when I pass it to the function, it throws an exception:

TypeError: Value does not implement interface Node.

How can I make it a true Node? Note that I cannot append it anywhere into the DOM!

Share Improve this question asked May 28, 2013 at 8:32 MarioDSMarioDS 13.1k15 gold badges70 silver badges123 bronze badges 3
  • @billyonecan oh, that did it. I should have seen the square brackets in firebug :) – MarioDS Commented May 28, 2013 at 8:37
  • 2 @billyonecan: Did you meant .get(0)?? – palaѕн Commented May 28, 2013 at 8:37
  • @PalashMondal .get()[0] give same result as .get(0) of course but the usual syntax is to pass index as param – A. Wolff Commented May 28, 2013 at 8:42
Add a ment  | 

1 Answer 1

Reset to default 5

I dont know codemirror and not sure what you call a true node. But to return the first DOM element matched, you need to use get(0):

var widget = $('<span class="ct-description-widget">' + descr + '</span>').get(0);

This is equivalent to:

var widget = $('<span class="ct-description-widget">' + descr + '</span>')[0];

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745267818a4619563.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信