使用innerHTML以外的方式操作包含文本和其他节点的DOM节点

发布于 2024-12-01 05:18:50 字数 1029 浏览 5 评论 0原文

我想知道是否有一种方法可以复制 DOM 节点,以便既可以切片节点的文本部分,又可以保持该节点中包含的节点完整。我有一个函数,它接受一个段落,将第一个字母与其分开,然后创建一个新节点,将包裹在新节点中的字母添加到其中,然后插入其余的文本。问题是,当该段落包含其他 HTML 元素时,它们会被展平为字符串,因为我使用 innerHTML 提取文本。我需要它们保留 DOM 节点。这是函数:

function createDropCappedParagraph(article) {
    pars = article.getElementsByTagName("p");
    first_par = pars[0];
    var text = first_par.innerHTML;
    text = text.trim();
    var first_letter = text.substr(0,1)
    text = text.slice(1);
    var t = document.createTextNode(text);
    var dropcap = document.createElement("span");
    dropcap.className = "dropcap";
    dropcap.innerHTML = first_letter
    var dcpar = document.createElement("p");
    dcpar.style.position = "relative";
    dcpar.appendChild(dropcap);
    dcpar.appendChild(t);
    article.insertBefore(dcpar, pars[0]);
    article.removeChild(pars[1]);
}

这是应用此效果时的样子,请注意第一段中扁平化的 a href 链接:

http://legibilis.heroku.com/start-here

谢谢, 詹姆斯

I'd like to know if there is a way to copy DOM nodes so that one can both slice the textual part of a node and keep the nodes contained within that node intact. I have a function that takes a paragraph, separates the first letter from it, then creates a new node, adds to it that letter wrapped in a new node, then inserts the rest of the text. The trouble is, when that paragraph contains other HTML elements they get flattened to a string because I extract the text using innerHTML. I need them to remain DOM nodes. Here's the function:

function createDropCappedParagraph(article) {
    pars = article.getElementsByTagName("p");
    first_par = pars[0];
    var text = first_par.innerHTML;
    text = text.trim();
    var first_letter = text.substr(0,1)
    text = text.slice(1);
    var t = document.createTextNode(text);
    var dropcap = document.createElement("span");
    dropcap.className = "dropcap";
    dropcap.innerHTML = first_letter
    var dcpar = document.createElement("p");
    dcpar.style.position = "relative";
    dcpar.appendChild(dropcap);
    dcpar.appendChild(t);
    article.insertBefore(dcpar, pars[0]);
    article.removeChild(pars[1]);
}

and here's what it looks like when this effect is applied, notice the flattened a href links in the first paragraph:

http://legibilis.heroku.com/start-here

Thanks,
James

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

岁月静好 2024-12-08 05:18:50

您的问题是,您使用此字符串创建 TextNode,因此所有特殊字符(例如 <、>、" 等)都会被转义(意味着它们会转换为安全的对应字符) 、" 等)

<> 创建一个TextNode 名为 t,创建一个名为 t 的普通 HTML 节点,例如,将其设为“div”标签,然后修改该标签的 .innerHTML 字段:t。 ,这不应该转义内容,并且 div 标签也不会给您带来太多麻烦(默认情况下它是内联的)。

.innerHTML = text;。据我所知
如果您使用 jQuery,$(text) 将为您提供 text 内容的 jQuery 元素(或多个元素)。

Your problem is that you use this string to create a TextNode, and therefore all special characters (such as <, >, ", etc.) get escaped (meaning they are converted to their safe counterparts: <, >, ", etc.).

Here's an idea for a simple workaround:

Instead of creating a TextNode called t, create a normal HTML node called t. For example, make this a 'div' tag. Then, modify this tag's .innerHTML field: t.innerHTML = text;. This shouldn't escape the content, AFAIK, and the div tag shouldn't bother you much (it's inline by default).

EDIT:
If you're using jQuery, $(text) will give you the jQuery element (or elements) for the content of text.

情深如许 2024-12-08 05:18:50

成功了,谢谢:

function createDropCappedParagraph(article) {
    pars = article.getElementsByTagName("p");
    first_par = pars[0];
    var text = first_par.innerHTML;
    text = text.trim();
    var first_letter = text.substr(0,1)
    text = text.slice(1);
    var t = document.createElement("span");
    t.innerHTML = text;
    var dropcap = document.createElement("span");
    dropcap.className = "dropcap";
    dropcap.innerHTML = first_letter
    var dcpar = document.createElement("p");
    dcpar.style.position = "relative";
    dcpar.appendChild(dropcap);
    dcpar.appendChild(t);
    article.insertBefore(dcpar, pars[0]);
    article.removeChild(pars[1]);
}

It worked, thank you:

function createDropCappedParagraph(article) {
    pars = article.getElementsByTagName("p");
    first_par = pars[0];
    var text = first_par.innerHTML;
    text = text.trim();
    var first_letter = text.substr(0,1)
    text = text.slice(1);
    var t = document.createElement("span");
    t.innerHTML = text;
    var dropcap = document.createElement("span");
    dropcap.className = "dropcap";
    dropcap.innerHTML = first_letter
    var dcpar = document.createElement("p");
    dcpar.style.position = "relative";
    dcpar.appendChild(dropcap);
    dcpar.appendChild(t);
    article.insertBefore(dcpar, pars[0]);
    article.removeChild(pars[1]);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文