XHTML中的document.createElement()问题 [英] Problems with document.createElement() in XHTML

查看:99
本文介绍了XHTML中的document.createElement()问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我在Firefox和其他现代浏览器中尝试以下内容:


window.addEventListener(''load'',function(){

document.title = CSS.getClass(''fontSize'');

var div = document.createElement(''div'');

document.getElementsByTagName(' 'body'')[0] .appendChild(div);

alert(div);

alert(div.style)

} ,true);


它在普通的HTML模式下工作正常(内容类型:text / html),但是在

XHTML模式下它会提醒[对象元素]而不是[object

HTMLDivElement]"并且第二个警报显示未定义而不是

" [object CSSStyleDeclaration]" ;.所以我无法达到风格声明

这对我来说非常重要。严格的模式一次又一次地麻烦,

最大的错误:document.write不起作用:
http://www.intertwingly.net/blog/200...hats-Not-Write


有人可以帮我解决我的问题吗?


Andi

解决方案



webEater写道:


您好,我在Firefox和其他现代浏览器中尝试以下内容:


window.addEventListener(''load'',function(){

document.title = CSS.getClass(''fontSize'');

var div = document.createElement(''div'');

document.getElementsByTagName(''body'')[0] .appendChild(div);

alert(div);

alert(div.style)

},true);



您的缩进令人困惑。请在下次清理它。


>

它在普通的HTML模式下工作正常(内容类型:文本/ html),但在

XHTML模式下,它会警告[object Element]而不是[object

HTMLDivElement]"并且第二个警报显示未定义而不是

" [object CSSStyleDeclaration]" ;.所以我无法达到样式声明



div没有任何样式属性;为什么第二次提醒显示

什么?它可能从样式表中应用了样式,但这不是

与样式属性相同。同样在严格的XHTML模式下,你应该使用node.getAttribute(''style'')而不是node.style来
。记住你是处理XML文档而不是HTML文档的
。 XML DOM与HTML DOM略有不同,可能值得

审查差异。


最大的bug:document.write不起作用:



不是错误。不要在严格的模式下使用document.write。


如果严格模式的额外挑战太难,我可以问你b $ b问你为什么''重新使用严格模式?为什么不使用普通的HTML模式,

如果这对你有用?


-David





11月25日21:36,David Golightly < davig ... @ gmail.comwrote:


webEater写道:


你好,我试过Firefox和其他现代浏览器中的以下内容:


window.addEventListener(''load'',function(){

document.title = CSS.getClass(''fontSize'');

var div = document.createElement(''div'');

document.getElementsByTagName( ''body'')[0] .appendChild(div);

alert(div);

alert(div.style)

},真的);你的缩进令人困惑。请在下次清理它。




它在普通的HTML模式下工作正常(内容类型:text / html),但在

它警告的XHTML模式[object Element]而不是[object

HTMLDivElement]"并且第二个警报显示未定义而不是

" [object CSSStyleDeclaration]" ;.所以我无法达到风格声明,没有任何风格属性;为什么第二个警报显示



什么?它可能从样式表中应用了样式,但这不是

与样式属性相同。同样在严格的XHTML模式下,你应该使用node.getAttribute(''style'')而不是node.style来
。记住你是处理XML文档而不是HTML文档的
。 XML DOM与HTML DOM略有不同,可能值得

审查差异。


最大的bug:document.write不起作用:不是bug。不要在严格模式下使用document.write。



如果严格模式的额外挑战太难,我可以问你为什么要使用严格模式吗?为什么不使用普通的HTML模式,

如果这对你有用?


-David



你好大卫,


我正在研究一个必须在每种模式下工作的框架,html和xml。


最大的bug:document.write不起作用:不是bug。不要在严格模式下使用document.write。



这是非常有根据的*笑。我写了一个解决方法并使用

它仍然;)我把它称为bug。


但是对于我的问题,我测试了所有可能的添加一个风格

我的div的声明:


window.addEventListener(''load'',function(){

文件.title = CSS.getClass(''fontSize'');

var div = document.createElement(''div'');

document.getElementsByTagName('' body'')[0] .appendChild(div);

alert(div);

//蛮力

// div。 style.backgroundColor =''red'';

div.getAttribute(''style'')。backgroundColor =''red'';

div.getAttribute(' 'style'')。setAttribute(''background Color'',''red'');

div.getAttribute(''style'')。setAttribute(''background-color'' ,''red'');

},true);


它们都不起作用。我认为必须有一种方式来添加一种风格

声明。这是我第一次遇到这个问题。 FireBug

错误:div.getAttribute(" style")没有属性




webEater写道:


window.addEventListener(''load'',function(){

document.title = CSS.getClass( ''fontSize'');

var div = document.createElement(''div'');

document.getElementsByTagName(''body'')[0] .appendChild(div);

alert(div);

//蛮力

//div.style.backgroundColor =''红色'';

div.getAttribute(''style'')。backgroundColor =''red'';

div.getAttribute(''style'')。setAttribute (''background Color'',''red'');

div.getAttribute(''style'')。setAttribute(''background-color'',''red'') ;

},真实);


它们都不起作用。我认为必须有一种方式来添加一种风格

声明。这是我第一次遇到这个问题。 FireBug

错误:div.getAttribute(" style")没有属性



数字1:再次,请修复你的缩进!我在上一篇文章的

中指出了这一点,你重新发布代码而不修复它。这不是

ok。


哇,您需要获取XML DOM的指南,并且快速。这不是
如何添加样式属性 - getAttribute返回一个值,而不是

引用,所以试图以这种方式设置其属性不会

工作。您还应该学习一些基本的OOP语言,如C ++或Java

,以了解getX和setX是DOM

在这里使用的基本属性接口,通常用于值,不是参考。即:

你需要打电话


div.setAttribute(''style'',''background-color:red; border:1px solid

green;'');


Hello, I try the following in Firefox and other modern browsers:

window.addEventListener(''load'', function() {
document.title = CSS.getClass(''fontSize'');
var div = document.createElement(''div'');
document.getElementsByTagName(''body'')[0].appendChild(div);
alert(div);
alert(div.style)
}, true);

It works fine in normal HTML mode (Content-type: text/html), but in
XHTML mode it alerts "[object Element]" instead of "[object
HTMLDivElement]" and the second alert shows "undefined" instead of
"[object CSSStyleDeclaration]". So I can''t reach the style declaration
which is important for me. Strict mode makes trouble again and again,
the biggest bug: document.write does not work:
http://www.intertwingly.net/blog/200...hats-Not-Write

Can somebody help me with my problem?

Andi

解决方案


webEater wrote:

Hello, I try the following in Firefox and other modern browsers:

window.addEventListener(''load'', function() {
document.title = CSS.getClass(''fontSize'');
var div = document.createElement(''div'');
document.getElementsByTagName(''body'')[0].appendChild(div);
alert(div);
alert(div.style)
}, true);

Your indentation is confusing. Please clean it up next time.

>
It works fine in normal HTML mode (Content-type: text/html), but in
XHTML mode it alerts "[object Element]" instead of "[object
HTMLDivElement]" and the second alert shows "undefined" instead of
"[object CSSStyleDeclaration]". So I can''t reach the style declaration

div doesn''t have any style attribute; why should the second alert show
anything? It may have styles applied from a stylesheet, but that''s not
the same as a style attribute. Also in strict XHTML mode you should
use node.getAttribute(''style'') rather than node.style. Remember you''re
dealing with an XML document, not an HTML document. The XML DOM is
eversoslightly different than the HTML DOM and it might be worth
reviewing the differences.

the biggest bug: document.write does not work:

Not a bug. Don''t use document.write in strict mode.

If the additional challenges of strict mode are too difficult, may I
ask why you''re using strict mode? Why not just use normal HTML mode,
if that''s working for you?

-David




On 25 Nov., 21:36, "David Golightly" <davig...@gmail.comwrote:

webEater wrote:

Hello, I try the following in Firefox and other modern browsers:

window.addEventListener(''load'', function() {
document.title = CSS.getClass(''fontSize'');
var div = document.createElement(''div'');
document.getElementsByTagName(''body'')[0].appendChild(div);
alert(div);
alert(div.style)
}, true);Your indentation is confusing. Please clean it up next time.



It works fine in normal HTML mode (Content-type: text/html), but in
XHTML mode it alerts "[object Element]" instead of "[object
HTMLDivElement]" and the second alert shows "undefined" instead of
"[object CSSStyleDeclaration]". So I can''t reach the style declarationdiv doesn''t have any style attribute; why should the second alert show

anything? It may have styles applied from a stylesheet, but that''s not
the same as a style attribute. Also in strict XHTML mode you should
use node.getAttribute(''style'') rather than node.style. Remember you''re
dealing with an XML document, not an HTML document. The XML DOM is
eversoslightly different than the HTML DOM and it might be worth
reviewing the differences.

the biggest bug: document.write does not work:Not a bug. Don''t use document.write in strict mode.


If the additional challenges of strict mode are too difficult, may I
ask why you''re using strict mode? Why not just use normal HTML mode,
if that''s working for you?

-David

Hello David,

I am working on a framework that must work in every mode, html and xml.

the biggest bug: document.write does not work:Not a bug. Don''t use document.write in strict mode.

This is very well-founded *laugh. I have written a workaround and use
it still ;) And I call it a bug.

But to my problem, I have tested all possibilities to add a style
declaration to my divs:

window.addEventListener(''load'', function() {
document.title = CSS.getClass(''fontSize'');
var div = document.createElement(''div'');
document.getElementsByTagName(''body'')[0].appendChild(div);
alert(div);
// brute force
//div.style.backgroundColor = ''red'';
div.getAttribute(''style'').backgroundColor = ''red'';
div.getAttribute(''style'').setAttribute(''background Color'', ''red'');
div.getAttribute(''style'').setAttribute(''background-color'', ''red'');
}, true);

None of them is working. I think there MUST be a way to add a style
declaration. It''s the first time that I have this problem. The FireBug
error: div.getAttribute("style") has no properties



webEater wrote:

window.addEventListener(''load'', function() {
document.title = CSS.getClass(''fontSize'');
var div = document.createElement(''div'');
document.getElementsByTagName(''body'')[0].appendChild(div);
alert(div);
// brute force
//div.style.backgroundColor = ''red'';
div.getAttribute(''style'').backgroundColor = ''red'';
div.getAttribute(''style'').setAttribute(''background Color'', ''red'');
div.getAttribute(''style'').setAttribute(''background-color'', ''red'');
}, true);

None of them is working. I think there MUST be a way to add a style
declaration. It''s the first time that I have this problem. The FireBug
error: div.getAttribute("style") has no properties

Number 1: Again, please fix your indentation! I pointed this out in
my last post and you repost your code without fixing it. This is not
ok.

Wow, you need to pick up a guide to the XML DOM, and quick. This isn''t
how you add a style attribute - getAttribute returns a value, not a
reference, so trying to set its properties in this way is not going to
work. You should also learn some basic OOP language like C++ or Java
to learn that getX and setX are basic property interfaces that the DOM
is using here that generally work in values, not references. To wit:
you need to call

div.setAttribute(''style'', ''background-color: red; border: 1px solid
green;'');


这篇关于XHTML中的document.createElement()问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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