本文介绍了从DIV获取正确的xhtml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨伙计们,我正面临着关于innterHTML失败的错误,而

读取DIV内部的xhtml内容,实际上它已经传递为html

删除关闭一些节点。有没有办法完整地阅读div中的内容

它是如何在页面中的那种:


var obj = document.getElementById(" ; myDIV");

var realContent = obj.toString();


非常感谢,chr

解决方案





使用XML Serializer对象或构建自己的序列化程序。由于迄今为止完全支持XHTML的唯一

UA是基于Gecko的(Uzo / 5.0) -

IE确实根本不支持它! - ,那将是Gecko DOM


<?xml version =" 1.0" encoding =" ISO-8859-1"?>

<!DOCTYPE html PUBLIC" - // W3C // DTD XHTML 1.0 Strict // EN"

http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

< html xmlns =" http://www.w3。 org / 1999 / xhtml">

< head>

< title> XMLSerializer示例(application / xhtml + xml)< / title>

< meta http-equiv =" Content-Script-Type" content =" text / javascript" />

< script type =" text / javascript">

<![CDATA [

函数isMethodType(s)

{

return(s ==" function" || s ==" object");

}


函数getSerialized(s)

{

var obj;

if(isMethodType(typeof document.getElementById)

&&(obj = document.getElementById(s))

&& typeof XMLSerializer ==" object" ;)

{

var o = new XMLSerializer();

if(o&& typeof o.serializeToString =="函数")

{

var realContent = o.serializeToString(obj);

alert(realContent);

}

}

}

]]>

< / script>

< / head>

< body>

< div id =" foo">< img src =" bar.png" alt =" bar" />< / div>

< div>

< input type =" button" value ="获取序列化内容"

onclick =" getSerialized(''foo'');" />

< / div>

< / body>

< / html>


请注意,这将添加文档的xmlns属性值'root $

元素到根元素的序列化字符串,以使

标记有效。


WFM在


Mozilla / 5.0(X11; U; Linux i686; en-US; rv:1.8)Gecko / 20051217

Debian / 1.5.dfsg-2 Firefox / 1.5 Mnenhy / 0.7.3.0


有什么好处,它也适用于Valid HTML 4.01;唯一的

差异是元素类型是大写的,这意味着它们必须在再次生成有效的XHTML之前降低,因为它区分大小写。


XHTML序列化结果的特殊性(或错误?)

作为application / xhtml + xml在


中Mozilla / 5.0(X11; U; Linux i686; en-US; rv:1.7.12)Gecko / 20051007

Debian / 1.7.12-1


是每个元素类型都有前缀,添加的'xmlns''属性

名称后缀为a0:。

HTH


PointedEars





前缀为a0:,后缀为:a0。

PointedEars

Hi guys, I''m facing the bug about the failure of innterHTML while
reading xhtml content inside a DIV, in fact it has passed as html
removing the closing of some nodes. Is there a way to read the content
of the div perfectly how it is in the page, kind of:

var obj = document.getElementById("myDIV");
var realContent = obj.toString();

Thanks a lot, chr

解决方案




Use an XML Serializer object or build your own serializer. Since the only
UAs that fully support XHTML to date are Gecko-based ones (Mozilla/5.0) --
IE does _not_ support it at all! --, that would be in the Gecko DOM

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>XMLSerializer Example (application/xhtml+xml)</title>
<meta http-equiv="Content-Script-Type" content="text/javascript"/>
<script type="text/javascript">
<![CDATA[
function isMethodType(s)
{
return (s == "function" || s == "object");
}

function getSerialized(s)
{
var obj;
if (isMethodType(typeof document.getElementById)
&& (obj = document.getElementById(s))
&& typeof XMLSerializer == "object")
{
var o = new XMLSerializer();
if (o && typeof o.serializeToString == "function")
{
var realContent = o.serializeToString(obj);
alert(realContent);
}
}
}
]]>
</script>
</head>
<body>
<div id="foo"><img src="bar.png" alt="bar"/></div>
<div>
<input type="button" value="Get Serialized Content"
onclick="getSerialized(''foo'');"/>
</div>
</body>
</html>

Note that this will add the `xmlns'' attribute value of the document''s root
element to the serialization string of the root element in order to make
the markup valid.

WFM in

Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8) Gecko/20051217
Debian/1.5.dfsg-2 Firefox/1.5 Mnenhy/0.7.3.0

What is nice is that it works with Valid HTML 4.01 as well; the only
difference is that element types are uppercased, meaning that they must
be lowercased before being valid XHTML again as that is case-sensitive.

A peculiarity (or a bug?) of the result of the serialization of XHTML
served as application/xhtml+xml in

Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.12) Gecko/20051007
Debian/1.7.12-1

is that each element type is prefixed, and the added `xmlns'' attribute
name is suffixed with "a0:".
HTH

PointedEars




Prefixed with "a0:", suffixed with ":a0".
PointedEars


这篇关于从DIV获取正确的xhtml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 04:49