本文介绍了innerHTML和SCRIPT标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问候......


我偶然发现了一个小问题。


我使用Ajax检索我需要的部分页面更新。我用另一页上的HTML内容更新了DIV

元素。


工作正常。


但是HTML有一个浏览器应该处理的SCRIPT标签,但是

它没有。


这是一个例子:


--- pageX.aspx ---

< table>

< tr>

< td>表01< / td>

< / tr>

< / table>

< script>

alert(" HI");

< / script>

--- end pageX.aspx ---


---浏览器页面---

< div id =" divContents">< / div>


< script>

divContents.innerHTML = getHtmlFromPage(" pageX.aspx");

< / script>

---浏览器上的结束页面---


当prowser获得pageX.aspx时并更新

''divContents''的内容显示表格,但它没有处理脚本。


我在做什么错了?


问候,


PJ


Greetings...

I have stumbled upon a small problem.

I use Ajax to retrieve part of a page I need to update. I update a DIV
element with the HTML contents I get from another page.

It works fine.

However the HTML have a SCRIPT tag that the browser should process, but
it doesn''t.

Here''s an example:

--- pageX.aspx ---
<table>
<tr>
<td>Table 01</td>
</tr>
</table>
<script>
alert("HI");
</script>
--- end pageX.aspx ---

--- page on browser ---
<div id="divContents"></div>

<script>
divContents.innerHTML = getHtmlFromPage("pageX.aspx");
</script>
--- end page on browser ---

When the prowser gets the "pageX.aspx" and updates the contents of the
''divContents'' it displays the table, but it didn''t process the script.

What am I doing wrong?

Regards,

PJ
http://pjondevelopment.50webs.com

推荐答案



它适用于IE,它不能在任何其他浏览器中工作,因为你依靠IE快捷方式获取
对div标签的引用。

It works in IE, it won''t work in any other browser since you are relying
on an IE-shortcut to get a reference to the div tag.



通过innerHTML插入的脚本块不能在任何浏览器中执行
除了NS6之外的



< snip>

Script blocks inserted via innerHTML don''t get executed in any browser
other than NS6

<snip>



您将需要搜索HTML块并找到脚本

元素并使用createElement插入它们以获取脚本块

已执行。


var d =

document.getElementById(''divContents'')。getElements ByTagName(" script")

var t = d.length

for(var x = 0; x< t; x ++){

var newScript = document.createElement (''script'');

newScript.type =" text / javascript" ;;

newScript.text = d [x] .text;

document.getElementById(''divContents'')。appendChild(newScript);


}

-

Randy

comp.lang.javascript常见问题 - &新闻组每周

暂时在:

Javascript最佳实践 -

You will have to search through your HTML block and find the script
elements and insert them using createElement to get the script blocks
executed.

var d =
document.getElementById(''divContents'').getElements ByTagName("script")
var t = d.length
for (var x=0;x<t;x++){
var newScript = document.createElement(''script'');
newScript.type = "text/javascript";
newScript.text = d[x].text;
document.getElementById(''divContents'').appendChild (newScript);

}
--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Temporarily at: http://members.aol.com/_ht_a/hikksnotathome/cljfaq/
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/




之前我还没有见过这种方法。我一直采取另一种方法

使用eval并想知道一种方式是否优越(更快,更强大

等)。


函数更新(元素,html){

document.getElementById(element).innerHTML = html;


var re = /< script \b [\\\\ S] *?>([\\\] *?)< \ // ig;

var match;

while(match = re.exec(html)){

eval(匹配[1]);

}

} ;


谢谢,

彼得

I haven''t seen this method before. I''ve been taking another approach
that uses eval and wonder if one way is superiour (faster, more robust
etc).

function update(element, html) {
document.getElementById(element).innerHTML=html;

var re = /<script\b[\s\S]*?>([\s\S]*?)<\//ig;
var match;
while (match = re.exec(html)) {
eval(match[1]);
}
};

Thanks,
Peter




之前我还没有见过这种方法。我一直采取另一种方法

使用eval并想知道一种方式是否优越(更快,更强大

等)。


函数更新(元素,html){

document.getElementById(element).innerHTML = html;


var re = /< script \b [\\\\ S] *?>([\\\] *?)< \ // ig;

var match;

while(match = re.exec(html)){

eval(匹配[1]);

}

} ;


I haven''t seen this method before. I''ve been taking another approach
that uses eval and wonder if one way is superiour (faster, more robust
etc).

function update(element, html) {
document.getElementById(element).innerHTML=html;

var re = /<script\b[\s\S]*?>([\s\S]*?)<\//ig;
var match;
while (match = re.exec(html)) {
eval(match[1]);
}
};



它们不等同,所以比较无关紧要。如果你 - eval -

code - var - 将创建函数局部变量而不是全局变量

和函数声明将是内部函数而不是全局函数。


Richard。

They are not equivalent so comparison is irrelevant. If you - eval -
code - var - will create function local variables instead of global ones
and function declarations will be inner functions not global ones.

Richard.


这篇关于innerHTML和SCRIPT标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 16:41