开发者

In jQuery, what is selected with $('<div />', {text : $this.attr('title')})?

well, I'm quite new to jQuery and while browsing all the documentation I found a tutorial on jQuery site about developing plugins. While reading it and trying to understand, I found something I can't find answer for. The example in section 6.3 Data has such code in it:

var $this = $(this),
         data = $this.data('tooltip'),
         tooltip = $('<div />', {
           text : $this.attr('title')
         });

I understand that it is declaration of several variables on one line, however, the last one - tooltip - is the one I'm interested in. Can someone be patient with my ignorance and explain me what will be the content of tooltip variable 开发者_开发问答after processing that line?

Thank you in advance.


It creates a new div element, and passes the value of $this.attr('title') to jQuery.fn.text

More information: http://api.jquery.com/jQuery/#jQuery2

jQuery( html, props )

html: A string defining a single, standalone, HTML element (e.g. or ).

props: An map of attributes, events, and methods to call on the newly-created element.

and

As of jQuery 1.4, the second argument can accept a map consisting of a superset of the properties that can be passed to the .attr() method. Furthermore, any event type can be passed in, and the following jQuery methods can be called: val, css, html, text, data, width, height, or offset.


I think that

tooltip = $('<div />', {
           text : $this.attr('title')
         });

is equivalent to :

tooltip = $('<div />').text( $this.attr('title') );

If u're running under Firefox or chrome, you may be try console.log( ..any.. ). It's very helpful to see precisely a variable

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜