Ich habe eine App, wo ein Seitengenerator eine Startseite erstellt, die in der Datenbank gespeichert ist. Die gespeicherte Information ist nur die outerHTML
des DOM-Objekts. Um die Bearbeitungsfunktion zu erstellen, brauche ich eine inverse Funktion, die das DOM-Objekt aus den HTML-Informationen neu erstellt. Leider führt die Zuordnung der entschlüsselten Informationen zum Abschnitt outerHTML
zu nichts, da die Änderungen im DOM-Objekt ignoriert werden.So erstellen Sie aus einem HTML-Text ein DOM-Objekt
Meine Frage: Wie kann ich ein DOM-Objekt aus gespeicherten OuterHTML-Informationen neu erstellen? Wenn das unmöglich ist, was wäre der richtige Weg, um so etwas zu tun?
BEARBEITEN Vielen Dank für Ihre Antwort! Hier sind einige Codeschnipsel, die zeigen, was ich mache. Der Prototyp MainWidget erhält Informationen von der HTML-Seite & möchte die Informationen zuweisen. Benutzte Funktionen sind Teil eines anderen Objekts (Prototyp) namens Widgets.
var asserts = new Asserts();
var calls = new Calls();
var constants = new MainConst();
var strings = new Strings();
var widgets = new MainWidget();
MainWidget.method('assign2Widget', function (call, label, value, isValid) {
isValid = toBoolean(isValid), value = strings.toString(value);
if (calls.isValid(call) && strings.hasMinimalLength(label, 2)) {
var section = (!label.includes('Builder') // A marker, not an element!
&& !label.includes(constants.NewsTicker))
? widgets.getSgSectionByLabel(document, label)
: null;
value = strings.decode(decodeURIComponent(value)); //<<<< Code decoded from DB
if (asserts.isObject(section)) {
section.outerHTML = value;
} else if (label.includes(constants.Elements)) {
if (asserts.isValid(infos.Elements) && infos.Elements.length > 0) {
// HERE, THE CODE FROM DB SHOULD BE ADDED TO WIDGET.
// The "label" gives the label of the widget, and the "value" is
// what is to be assigned to.
}
} else if (label.includes(constants.NewsTicker)) {
[…]
}
[…]
});
Widget.method('getElementTypeByLabel', function (thing, elementType, label) {
if (asserts.isObject(thing)
&& strings.hasMinimalLength(elementType, 2)
&& strings.hasMinimalLength(label, 2)) {
var element, elements = document.getElementsByTagName(elementType);
if (asserts.isValid(elements)
&& asserts.isDefined(elements.length) && elements.length === 0) {
elements = thing.children;
}
for (var i = 0; asserts.isDefined(elements.length) && i < elements.length; i++) {
element = elements*;
if (element.getAttribute !== null) {
var labelName = element.getAttribute(constants.label);
if (label === labelName && element !== null) {
return element;
}
} else {
return null;
}
}
// If the element was not found, search recursively the elements!
for (var i = 0; asserts.isDefined(elements.length) && i < elements.length; i++) {
return this.getElementTypeByLabel(elements*, elementType, label);
}
} else {
// Error! //messages.invalidFunctionCall(functionName);
}
return null;
});
Widget.method('getSgSectionByLabel', function (element, label) {
return this.getElementTypeByLabel(element, 'sg-section', label);
});
EDIT 2: Früher habe ich nur die in den sg-section
vorgenommenen Änderungen den Inhalt neu zu erstellen, wenn eine vorhandene Seite bearbeiten.
MainWidget.js:
[…]
MainWidget.method('assign2Widget', function (call, label, value, isValid) {
isValid = toBoolean(isValid), value = strings.toString(value);
if (calls.isValid(call) && strings.hasMinimalLength(label, 2)) {
var section = (!label.includes('Builder') // A marker, not an element!
&& !label.includes(constants.NewsTicker))
? widgets.getSgSectionByLabel(document, label)
: null;
value = strings.decode(decodeURIComponent(value));
if (asserts.isObject(section)) {
section.outerHTML = value;
} else if (label.includes(constants.Elements)) {
if (asserts.isString(value) && value.includes(LeftBracket)) {
value = arrays.toArray(value); //value.replace(LeftBracket, '').split(RightBracket);
}
if (arrays.isArray(value)) {
for (var i = 0; i < value.length; i++) {
infos.insertGridItem(value[i]);
}
}
[…]
MainInfos.js:
[…]
function MainInfos() {
this.Elements; // Object for startGridGenerator
this.insertGridItem; // Function of startGridGenerator
this.PrototypeName = 'MainInfos';
}
MainInfos.inherits(Infos);
MainWidget.method('assign2Widget', function (call, label, value, isValid) {
isValid = toBoolean(isValid), value = strings.toString(value);
if (calls.isValid(call) && strings.hasMinimalLength(label, 2)) {
var section = (!label.includes('Builder') // A marker, not an element!
&& !label.includes(constants.NewsTicker))
? widgets.getSgSectionByLabel(document, label)
: null;
value = strings.decode(decodeURIComponent(value));
if (asserts.isObject(section)) {
section.outerHTML = value;
} else if (label.includes(constants.Elements)) {
if (asserts.isString(value) && value.includes(LeftBracket)) {
value = arrays.toArray(value); //value.replace(LeftBracket, '').split(RightBracket);
}
if (arrays.isArray(value)) {
for (var i = 0; i < value.length; i++) {
infos.insertGridItem(value[i]);
}
}
} else if (label.includes(constants.NewsTicker)) {
[…]
}
});
startGridGenerator.js:
[…]
(function() {
var moduleName = 'ivmGridBuilder';
var controllerName = moduleName + Controller;
angular.module(moduleName, [
'ionic',
'ui.sortable',
'ivmColorpicker',
'ivmIconpicker'
])
.controller(controllerName, function ($scope,
$ionicScrollDelegate,
$ionicModal,
$sce,
CommunicationService) {
[…]
/**
* Inserts an existing grid item to the item's array
*
* @param item
*/
$scope.insertGridItem = function (item) {
if (asserts.isObject(item)) {
$scope.items.push(item);
infos.Elements.push(item);
$ionicScrollDelegate.resize()
} else {
// First call with no valid item & "remember" this function
// by having a link to in in object MainInfos!
infos.insertGridItem = $scope.insertGridItem;
}
};
Danke! Ich werde versuchen, ob ich das verwenden kann, um das leere 'sg-section'-Objekt im DOM durch die gespeicherte' sg-section' 'outerHTML'-Information zu ersetzen. – Sae1962