<分区>
也许这是非常基本的,但我很困惑。 我有一个包含许多部分 (div) 的简单 html 页面。我在 javascript 中有一个包含 html 标签的字符串。代码如下:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript">
var str1="<html><body><div id='item1'><h2>This is a heading1</h2><p>This is a paragraph1.</p></div><div id='item2'><h2>This is a heading2</h2><p>This is another paragraph.</p></div><div id='lastdiv'>last</div></body></html>";
</script>
</head>
<body>
<div id="title1"></div>
<div id="new1"></div>
<div id="title2"></div>
<div id="new2"></div>
</body>
</html>
我想提取 html 标签的内容(从 javascript 中的字符串)并在我的 html 页面中的所需部分显示该内容。
即我希望在 <div id="title1">
中显示“This is a heading1”和“这是段落1”。显示在<div id="new1">
第二对标签也一样。
我需要所有这些只在客户端工作。我曾尝试使用 HTML DOM getElementByTagName 方法,但它变得太复杂了。我对 jquery 知之甚少。我很困惑。我不知道该怎么做。你能指导我使用什么 - javascript 或 jquery 以及如何使用它吗?有没有办法从字符串中识别并遍历它?
如何从 str1 中提取“This is heading1”(以及包含在 html 标签中的类似内容)?我不知道这些索引,因此不能在 javascript 中使用 substr() 或 substring() 函数。