如何prevent jQuery.append()函数自动关闭< P>以< / P&GT ;? [英] How to prevent jQuery.append() function from automatically closing a <p> with </p>?

查看:246
本文介绍了如何prevent jQuery.append()函数自动关闭< P>以< / P&GT ;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在循环使用我追加单个单词到一个元素

In a loop I append single words to an element using

$(".somediv").append(wordArray[i]+" ");

在字阵中还有一些< P> < / P> 标记。如果他们的加入,jQuery的似乎与一个&LT自动关闭它们; / P>

In the word array there are some <p> and </p> tags. And if they are added, jQuery seems to close them automatically with a </p>.

如何从这样做,prevent呢?

How can I prevent it from doing that?

推荐答案

您不会追加与jQuery的标签,您将追加节点。您正在运行的文件是不是像一个字符串&LT; HTML&GT;&LT; HEAD&GT;&LT;标题&GT; T&LT; /标题&GT;&LT; /头&GT;&LT;身体GT;&LT; D​​IV ID =D &GT;&LT; p&GT; p&LT; / p&GT;&LT; / DIV&GT;&LT; /身体GT;&LT; / HTML&GT; 。它像

You don't append tags with jquery, you append nodes. The document you are operating on is not a string like <html><head><title>T</title></head><body><div id="D"><p>P</p></div></body></html>. It is a tree like

HTML Element
+---HEAD Element
|   `---TITLE Element
|       `---Text node, contents: "T"
`---BODY Element
    `---DIV Element, attributes: "id" => "D"
        `---P Element
            `---Text node, contents: "P"

有没有办法重新present树中的一个未关闭标签或未完成的元素。只有完整的元素存在。

There is no way to represent an "unclosed tag" or "unfinished element" in the tree. Only complete elements can exist.

文件撰写方法对文档作为字符串操作,并且它可以追加任意片段,但也有<一个href=\"http://stackoverflow.com/questions/802854/why-is-document-write-considered-a-bad-practice\">significant缺点它的使用。的从过渡DOM-0级标签思维现代DOM树思维是使用jQuery一个prerequisite。

The old document.write method operates on the document as a string, and it can append arbitrary fragments, but there are significant disadvantages to its use. The transition from DOM-level-0 "tag thinking" to modern DOM "tree thinking" is a prerequisite for using jquery.

您需要决定要如何树的时候添加了开始标记的时间和当添加结束标记时间之间的间隔的样子。这里是一个可能的答案:让添加的隐式结束标记,然后从一开始每次追加一次重建字符串,就像这样:

You need to decide how you want the tree to look in the interval between the time when the start tag is added and the time when the end tag is added. Here's one possible answer: let the implicit closing tag be added, and then rebuild the string from the start each time you append, like this:

$(".somediv").html(wordArray.slice(0,i+1).join(" "));

这样,如果 wordArray [富,&LT; P&gt;中,酒吧,巴兹,&LT; / p&gt;中,QUUX] 循环将工作是这样的:

That way if wordArray is ["foo","<p>","bar","baz","</p>","quux"] the loop will work like this:

i    string passed to html()        equivalent HTML with all tags shown
0    "foo"                          "foo"
1    "foo <p>"                      "foo<p></p>"
2    "foo <p> bar"                  "foo<p>bar</p>"
3    "foo <p> bar baz"              "foo<p>bar baz</p>"
4    "foo <p> bar baz </p>"         "foo<p>bar baz</p>"
5    "foo <p> bar baz </p> quux"    "foo<p>bar baz</p>quux"

请注意,当&LT; / P&GT; 从您的输入被添加,它没有任何效果。并增设了&LT的; P&GT; 并没有太多明显的效果无论是。因此,也许它会更好,如果 wordArray 每一个元素包含了一些文本: [富,&LT; P&GT;酒吧,巴兹&LT; / p&gt;中,QUUX]

Note that when the </p> from your input gets added, it has no effect. And the addition of the <p> doesn't have much visible effect either. So maybe it would be better if every element of wordArray included some text: ["foo","<p>bar","baz</p>","quux"]

它看起来有点缺憾,但是这就是你对尝试使用不完整的HTML元素作为输入格式!的树思维!作为未来!

It looks a little kludgy, but that's what you get for trying to use incomplete HTML elements as your input format! Tree thinking! For the future!

这篇关于如何prevent jQuery.append()函数自动关闭&LT; P&GT;以&lt; / P&GT ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆