开发者

In jquery what does $('<div></div>') do?

开发者 https://www.devze.com 2023-01-29 02:13 出处:网络
I was implementing a jqueryui modal dialog box and a related blog showed a way of implementing it that worked but I don\'t understand what $(\"<div><开发者_开发技巧;/div>\") is actually doi

I was implementing a jqueryui modal dialog box and a related blog showed a way of implementing it that worked but I don't understand what $("<div><开发者_开发技巧;/div>") is actually doing. Is this creating a blank div element to use? Is this a safe way of implementing this?


It's equivalent to document.createElement('DIV').

You can look at the jQuery source. Look for the comment:

// Handle HTML strings

And you'll see how it works.

The <div> created is empty, has no attributes, and is not attached to the DOM.

It's more common to see it written as:

$('<div />')

...but not functionally different.

Often it's chained with a method like appendTo(), to insert it into the DOM.


It's creating a blank div to use. I don't see why it wouldn't be considered safe. It's valid markup. What concerns do you have about it specifically?


It is safe but kind of outdated, this is better to use:

$("<div/>");

or optionally, you can predefine its content, event handlers etc.:

$("<div/>",{
  "html"  : "some content",
  "click" : function(){
    //do something.
  }
});
0

精彩评论

暂无评论...
验证码 换一张
取 消