帮我做正确的DOM事情。 [英] Help me do the right DOM thing.

查看:147
本文介绍了帮我做正确的DOM事情。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


我已经读过DOM纯粹主义者不喜欢document.write

和innerHTML的所有原因。我想成为一名优秀的程序员并做正确的事情并且不使用它们。但是我需要你的帮助来完成

项目需要完成的工作。


为了简洁起见,以下代码已经简化了。 />
你会注意到sDoc包含HTML和JavaScript,这是一个不可更改的要求。请不要浪费你的时间或我的时间。

解释它是多么无聊。我知道它是,但我没有什么可以做的。答案严格关注可以做什么与什么应该做什么应该非常感谢。


有三种尝试的方法来渲染sDoc。第一个是

来创建一个textNode并将其附加到div。它按照预期将代码打印为

文本而不是呈现它。是否可以将它转换为

渲染?


第二种方法使用innerHTML并呈现HTML而不是

JavasScript。


第三种方法使用document.write来渲染HTML和

JavaScript,但我们真的不想用这个写方法,

但是在这一点上看起来我们别无选择。


我们甚至试图在DOM创建的iFrame中渲染sDoc,但是它在Mozilla浏览器中窒息了

,因为包含脚本的已知错误

使用document.write(再次是另一个不可能的要求

已更改)将文件作为document.close失败。

< div id =" foo1" style =" border:1px solid black;">< / div>

< div id =" foo2" style =" border:1px solid black;">< / div>

< script type =" text / javascript">

var sDoc =''< center>< scr''+''ipt

language = javascript> alert(true);< / scr''+''ipt>< / center>' ';

var myText = document.createTextNode(sDoc);

document.getElementById(" foo1")。appendChild(myText);

document.getElementById(" foo2")。innerHTML = sDoc;

document.write(sDoc);

< / script>

感谢您的考虑。

Hello,

I have read all the reason why DOM purists don''t like document.write
and innerHTML. I would like to be a good programmer and do the right
thing and not use them. But I need your help to get it to what the
project needs done.

In the interest of brevity, the following code has been simplified.
You''ll notice sDoc contains HTML and JavaScript, this is a
unchangeable requirement. PLEASE don''t waste your time or mine by
explaining how un-couth it is. I know it is but there''s nothing I can
do about. Answers strictly focused what can be done versus what should
be done will be greatly appreciated.

There are three attempted methods to render the sDoc. The first one is
to create a textNode and append it to a div. It prints the code as
text instead of rendering it, as expected. Is it possible to get it to
render?

The second method uses innerHTML and the HTML is rendered but not the
JavasScript.

The third method uses document.write which renders both the HTML and
JavaScript, but we really really don''t want to use the write method,
but at this point it looks like we have no choice.

We''ve even tried to render sDoc in a DOM created iFrame, but it chokes
in Mozilla browsers because of the known bug of included scripts that
use document.write (once again another requirement that can''t be
changed) kills the page as document.close fails.
<div id="foo1" style="border:1px solid black;"></div>
<div id="foo2" style="border:1px solid black;"></div>
<script type="text/javascript">
var sDoc = ''<center><scr''+''ipt
language=javascript>alert(true);</scr''+''ipt></center>'';
var myText = document.createTextNode(sDoc);
document.getElementById("foo1").appendChild(myText );
document.getElementById("foo2").innerHTML = sDoc;
document.write(sDoc);
</script>

Thank you for your consideration.

推荐答案

我已经阅读了DOM纯粹主义者不喜欢document.write $的所有理由b $ b
I have read all the reason why DOM purists don''t like document.write

和innerHTML。我想成为一名优秀的程序员并做正确的事情并且不使用它们。但是我需要你的帮助来实现

项目需要完成的工作。
and innerHTML. I would like to be a good programmer and do the right
thing and not use them. But I need your help to get it to what the
project needs done.



OK

OK


为简洁起见,以下代码已经简化。

您会注意到sDoc包含HTML和JavaScript,这是一个不可更改的要求。请不要浪费你的时间或我的时间。

解释它是多么无聊。
In the interest of brevity, the following code has been simplified.
You''ll notice sDoc contains HTML and JavaScript, this is a
unchangeable requirement. PLEASE don''t waste your time or mine by
explaining how un-couth it is.



所以我想听听关于正确的事情,但我不会给你一个该死的

可能会说任何我会继续做这些事情我是

习惯于。

好​​吧,我个人不会浪费任何人的时间建议。 :-): - |


P.S.如果这是一个论战帖子来陈述你自己在主题上的立场

那么你至少可以选择比
更奇怪的东西

So "I want to hear about right things but I don''t give a damn that you
may say as in any case I''ll keep doing the stuff in the way I''m
accustomed to".
OK, I personally will not waste no one''s time as suggested. :-) :-|

P.S. If it was a polemic post to state your own position on the topic
then you could at least choose something lesser bizarre than


var sDoc =''< center>< scr''+''ipt

language = javascript> alert(true);< / scr''+''ipt>< / center>' ;
var sDoc = ''<center><scr''+''ipt
language=javascript>alert(true);</scr''+''ipt></center>'';



A< scripttag以古老的Netscape为中心< centertag?

中的名称是什么意思?如果它是你的页面之一的真实摘录,那么你有比任何动态的b $ b内容生成更严重的问题。如果你只是在激烈的争论中取得成功那么

它将有助于看到一个真实的生活案例。

A <scripttag centered by using ancient Netscape <centertag? What in
the name does it suppose to mean? If it''s a real extract from one of
your pages then you have much more serious problems than any dynamic
content generation. If you just made it up in a polemic excitement then
it would help to see a real life case.


你不能不要回答清教徒的非回答,我知道:-)


让我提问非常简单:YES或NO可以完成DOM

用HTML和JavaScript的字符串完成什么用的是什么?
IS!?!


我有
You couldn''t resist the puritan non-answer answer, I knew it :-)

Let me make the question very simple: YES or NO can DOM accomplish
what write accomplishes with the string of HTML and JavaScript AS IT
IS!?!

I''ve got


20美元骑在办公室打赌这个非常简单的问题

不会得到一个简单的回答不能做不到或者是的,甚至是

如果你这样做了,你可以让它工作,那就是......


来吧女孩,让我失去了我的钱。

20 dollars riding on an office bet this very simple question
won''t get answered with a simple "no it can''t be done" or "yes, even
with that mangled string you can GET IT TO WORK, if you did this..."

Come on guys and girls, make me lose my money.


这篇关于帮我做正确的DOM事情。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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