jQuery-Objekt auf String anstatt auf Dokument beziehen

Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

  • jQuery-Objekt auf String anstatt auf Dokument beziehen

    Heyho ;D.

    Ich arbeite an einem HTML5-konformen Quellcode-Generator, für den ich auch die Datenbank brauchte.
    Gibt es eine Möglichkeit, ein jQuery-Objekt zu erstellen, dass sich auf meinen generierten Quellcode anstatt auf das aktuelle Dokument bezieht?

    Im Klartext soll es in etwa so aussehen:

    Quellcode

    1. var myJQ = new jQuery("<html><head></head><body><div id='hello_world'></div></body></html>");
    2. myJQ('#hello_world').html("Hello World! :)");


    Gruß,
    SargTeX
  • Das funktioniert nur halb.
    Beispiel für ein verschachteltes Element, wo der erste Knoten gesucht werden kann.
    $("<div id='hello_world'>hello<div id='foo'>foo</div></div>").find('#foo')

    Mehr Informationen unter api.jquery.com/jQuery/, Creating New Elements

    If a string is passed as the parameter to $(), jQuery examines the string to see if it looks like HTML (i.e., it has <tag ... > somewhere within the string). If not, the string is interpreted as a selector expression, as explained above. But if the string appears to be an HTML snippet, jQuery attempts to create new DOM elements as described by the HTML. Then a jQuery object is created and returned that refers to these elements. You can perform any of the usual jQuery methods on this object:

    Quellcode

    1. $('<p id="test">My <em>new</em> text</p>').appendTo('body');

    When the HTML is more complex than a single tag without attributes, as it is in the above example, the actual creation of the elements is handled by the browser's innerHTML mechanism. In most cases, jQuery creates a new <div> element and sets the innerHTML property of the element to the HTML snippet that was passed in. When the parameter has a single tag, such as $('<img />') or $('<a></a>'), jQuery creates the element using the native JavaScript createElement() function.

    When passing in complex HTML, some browsers may not generate a DOM that exactly replicates the HTML source provided. For example, Internet Explorer prior to version 8 will convert all href properties on links to absolute URLs, and Internet Explorer prior to version 9 will not correctly handle HTML5 elements without the addition of a separate compatibility layer.

    To ensure cross-platform compatibility, the snippet must be well-formed. Tags that can contain other elements should be paired with a closing tag:

    Quellcode

    1. $('<a href="http://jquery.com"></a>');

    Alternatively, jQuery allows XML-like tag syntax (with or without a space before the slash):

    Quellcode

    1. $('<a/>');

    Tags that cannot contain elements may be quick-closed or not:

    Quellcode

    1. $('<img />');
    2. $('<input>');
  • Im Prinzip schon, ich wüsste nur nicht, was das anderen bringen würde ;D.
    Es hat nicht direkt was mit jQuery zu tun, sondern ist eine eigene Implementierung für meinen Nutzen.

    Das ganze kommt in ein Plug-In von mir, mit dem es erstmal möglich sein soll, HTML-Dokumente zu erstellen. Der große Vorteil an dem Plug-In, das daher auch recht Umfangreich wird, ist, dass es zu 100% HTML5 konform ist (gemäß der, leider noch nicht ganz fertigen, W3C-Dokumentation).
    Gehe schon seit Tagen Dokumentationen vom W3C durch, um die absolut korrekte Unterstützung zu sichern :D.

    Auf dem Plug-In aufbauend soll dann später ein grafischer Editor entstehen, der neben W3C Konformität vor allem den Programmiervorgang beschleunigen soll. Das Tool soll für Entwickler ausgelegt sein und viele jQuery-Effekte bereits mitbringen, z.B. das "Accordion" oder die "Tabs" von jQueryUI. Großer Wert wird dabei vor allem auf Erweiterbarkeit und Individualität gelegt, d.h., der Entwickler soll möglichst überall eingreifen können.
    Das Toll soll kostenlos und OpenSource veröffentlicht werden und, wenn Interesse besteht, ein Gemeinschaftsprojekt von mehreren Programmierern werden.

    Wenn du (oder sonst jemand) weiteres Interesse daran hat, kann ich mal ein Google Code Projekt mit näheren Informationen erstellen ;).

    Gruß,
    SargTeX