Well, you could also do:
$("h1").prepend("§");
The reasoning behind handling selectors (and HTML) in $(...) and then later
appending/prepending/etc. them into the document is that you can modify them
in the interim.
For example:
$("<h1>Something</h1>").click(function(){ .... }).prependTo("div.section");
Whereas there's really no associated use case for pure text nodes.
--John
On Tue, Jul 14, 2009 at 6:03 PM, David Flanagan <[email protected]>wrote:
> I'd like to propose that the jQuery() function accept a simple new
> syntax to allow it to create simple text nodes. If the string argument
> begins with an apostrophe, then treat the rest of the argument as plain
> text and return a jQuery object that wraps a single text node.
>
> I ask for this because I was trying to do this:
>
> $("§").prependTo("h1");
>
> Then I realized that since my string doesn't contain any angle brackets
> it is being treated as a selector rather than as a string of HTML, and I
> have to write this:
>
> $(document.createTextNode("§")).prependTo("h1");
>
> I don't know how valuable this feature would be, but I've attached a
> simple (and barely tested) patch to implement it.
>
> With the patch, the code above gets a single apostrophe added to it and
> becomes:
>
> $("'§").prependTo("h1");
>
> David
>
> >
>
> Index: core.js
> ===================================================================
> --- core.js (revision 6415)
> +++ core.js (working copy)
> @@ -17,7 +17,7 @@
>
> // A simple way to check for HTML strings or ID strings
> // (both of which we optimize for)
> - quickExpr = /^[^<]*(<(.|\s)+>)[^>]*$|^#([\w-]+)$/,
> + quickExpr = /^[^<]*(<(.|\s)+>)[^>]*$|^#([\w-]+)$|^'(.*)$/,
>
> // Is it a simple selector
> isSimple = /^.[^:#\[\.,]*$/,
> @@ -51,24 +51,28 @@
>
> // Handle HTML strings
> if ( typeof selector === "string" ) {
> - // Are we dealing with HTML string or an ID?
> + // Are we dealing with HTML string or an ID or a
> text node?
> match = quickExpr.exec( selector );
>
> // Verify a match, and that no context was specified
> for #id
> - if ( match && (match[1] || !context) ) {
> + if ( match && (match[1] || match[4] || !context) )
> {
>
> // HANDLE: $(html) -> $(array)
> if ( match[1] ) {
> selector = jQuery.clean( [ match[1]
> ], context );
>
> - // HANDLE: $("#id")
> + // HANDLE: $("#id") and $("'text")
> } else {
> - elem = document.getElementById(
> match[3] );
> + if ( match[3] ) {
> + elem =
> document.getElementById( match[3] );
>
> - // Handle the case where IE and
> Opera return items
> - // by name instead of ID
> - if ( elem && elem.id !== match[3]
> ) {
> - return rootjQuery.find(
> selector );
> + // Handle the case where IE
> and Opera return items
> + // by name instead of ID
> + if ( elem && elem.id !==
> match[3] ) {
> + return
> rootjQuery.find( selector );
> + }
> + } else {
> + elem =
> document.createTextNode( match[4] );
> }
>
> // Otherwise, we inject the element
> directly into the jQuery object
>
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"jQuery Development" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/jquery-dev?hl=en
-~----------~----~----~----~------~----~------~--~---