gpt4 book ai didi

dom - 将分布式子项附加到 Polymer 中的 元素

转载 作者:行者123 更新时间:2023-12-02 13:22:52 27 4
gpt4 key购买 nike

我有一个简单的 polymer 元素:

<link rel="import" href="bower_components/polymer/polymer.html">


<dom-module id="observing-test">
<template>
<p>This is the title:</p>
<content id="top" select="#title"></content>
<p>This is the content:</p>
<content id="bottom" select="#content"></content>
</template>

<script>
Polymer( {
is: 'observing-test',

ready: function(){
console.log("Ready!");

Polymer.dom( this.$.top ).observeNodes( function( info ) {
console.log(" TOP CHANGED!", info );
});

Polymer.dom( this.$.bottom ).observeNodes( function( info ) {
console.log(" BOTTOM CHANGED!", info );
});
},

attached: function(){
console.log("Attached!");
OT = this;
}

})
</script>
</dom-module>

基本用法:

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="description" content="The basic page">
<meta name="author" content="Tony Mobily">
<title>The title</title>
<link rel="import" href="test-component.html">
</head>

<body>

<observing-test>
<div id="title"><p>TITLE!</p></div>
<div id="content"><p>CONTENT!</p></div>
</observing-test>

</body>

现在,任何添加到 #top#bottom 的内容都会被观察到。

那么,如何将子元素附加到 #top <content> 元素以便触发观察者?

如果我从本地 DOM (从控制台)到达该元素: MAYBE = Polymer.dom( OT ).children[0] (这将是 <div id="title"> ,然后是 Polymer.dom( MAYBE ) .appendChild( document.createElement( 'p' ) ) ,新元素将被添加到 <div> 。但是,观察者不会被触发(因为我将子级添加到 <div> 而不是 <div> 的父级,我认为这是正在观看的内容。)

所以问题:

  • 当观察本地 DOM 中的 <content> 元素时,如何从那里追加删除节点?基本上,我想向 <content> 的子元素添加一个额外的子元素,以便 OT.getContentChildren('#top') 返回一个额外的元素

  • 观察时,我是否会收到直接对节点子节点之一进行更改的通知?孙辈的修改不会冒泡,对吗?当定位 <content> 元素时,我实际上观察到了什么?

最佳答案

  • <content>是一个 DOM 插入点(我不会尝试详细解释它是什么,Polymer's dev guide 比我做得更好)。基本上,组件的每个轻 DOM 子级都与 select 匹配。查询<content>节点将被插入其中。

    因此,要在 <content> 中插入另一个子项节点,您必须添加一个与其 select 匹配的子节点查询组件的 light DOM。

    为此,您应该更改 select查询以 ID 以外的其他内容(即类或属性)为目标,如下所示:

在元素的 dom-module 中:

 <content id="top" select=".title"></content>

用法:

 <observing-test id="ot">
<p class=".title">TITLE!</p>
<div id="content"><p>CONTENT!</p></div>
</observing-test>

var newTitleNode = document.createElement( 'p' );
newTitleNode.className = "title";
Polymer.dom( this.$.ot ).appendChild( newTitleNode );
  • polymer 的observeNodes用于检测本地 DOM 插入点中节点的添加/删除。您在这里所做的是将一个节点附加到一个轻量 DOM 节点,该节点本身已插入 content 中。插入点,您不是直接插入另一个节点。我相信这就是您没有收到通知的原因。

希望这对您有帮助,某个地方的人可能可以比我更好地解释这一点。

关于dom - 将分布式子项附加到 Polymer 中的 <content> 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33990745/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com