也许这是非常基本的,但我都很困惑。 我有一个简单的HTML页面与许多部分(股利)。我有一个字符串包含javascript中的html标签。代码如下:如何使用javascript或jquery从字符串中提取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 paragraph1”中显示“This is a heading1”。在<div id="new1">
中显示,对于第二对标签也是如此。
我需要所有这些只在客户端工作。我试图使用HTML DOM getElementByTagName方法,它变得太复杂了。我对jquery知之甚少。我很困惑。我不明白如何去做。你能指导我使用什么 - JavaScript或jQuery和如何使用它?有没有一种方法来识别字符串并遍历它?
如何从str1中提取“This is heading1”(以及包含在html标签中的类似内容)?我不知道这些索引,因此不能在javascript中使用substr()或substring()函数。
Thnks ..它的工作.. – user1196522 2012-02-08 09:41:17