我使用HtmlAgilityPack和C#來轉換舊的IE標籤以及Javascript以與其他瀏覽器兼容。這裏有一個例子:使用HtmlAgilityPack設置InnerHtml屬性會產生意想不到的結果
舊代碼:
<script for="thisForm" event="onsubmit()" language="JScript">
var Checked = false
var Counter = 0
for (;Counter < this.choice.length; Counter++)
{
if (this.choice[Counter].checked)
{
Checked = true
this.action = this.choice[Counter].value
}
}
if (!Checked)
{
alert ("Please make a selection")
return false
}
</script>
我轉換爲:
<script ftype="text\JScript">
function thisForm_onsubmit(el)
{
var Checked = false
var Counter = 0
for (;Counter < el.choice.length; counter++)
{
if (el.choice[counter].checked)
{
checked = true
el.action = el.choice[counter].value
}
}
if (!checked)
{
alert ("please make a selection")
return false
}
}
</script>
我上面的,活動中移除的內容,和語言的腳本標籤屬性,添加了type = 「text/JScript」屬性並將javascript包裝成功能代碼。
我這樣做只需添加HtmlNode屬性,然後替換InnerHtml屬性值。到目前爲止,它工作正常,直到我遇到上述功能。不知何故,而不是給我上面的結果,我得到如下:
<script type="text/JScript">
function thisForm_onsubmit(el)
{
var Checked = false
var Counter = 0
for (;Counter < el.choice.length; counter++)
{
if (el.choice[counter].checked)
{
checked = true
el.action = el.choice[counter].value
}
}
if (!checked)
{
alert ("please make a selection")
return false
}
}
el.choice.length;="" counter++)="" {="" if="" (el.choice[counter].checked)="" {="" checked="true" el.action="el.choice[Counter].value" }="" }="" if="" (!checked)="" {="" alert="" ("please="" make="" a="" selection")="" return="" false="" }="" }=""></ el.choice.length; counter++)
{
if (el.choice[counter].checked)
{
checked = true
el.action = el.choice[counter].value
}
}
if (!checked)
{
alert ("please make a selection")
return false
}
}
></script>
奇怪的部份,我分配給的innerHTML文本是正確的,但scriptNode.InnerHtml表現出不同的價值
這裏是我的C#代碼:
if (scriptNode.Attributes["for"] != null)
{
{
if (scriptNode.Attributes["for"] != null)
ctrl = scriptNode.Attributes["for"].Value;
if (scriptNode.Attributes["event"] != null)
evt = scriptNode.Attributes["event"].Value;
if (scriptNode.Attributes["type"] != null)
typ = scriptNode.Attributes["type"].Value;
if (scriptNode.Attributes["language"] != null)
lang = scriptNode.Attributes["language"].Value;
if (scriptNode.InnerHtml != null)
code = scriptNode.InnerHtml;
func_name = ctrl + "_" + evt;
if (ctrl != "window")
new_script = Environment.NewLine + "function " + RemoveBrackets(func_name) + "(el)" + Environment.NewLine;
else
new_script = Environment.NewLine + "function " + AddBrackets(RemoveBrackets(func_name)) + Environment.NewLine;
new_script += "{" + Environment.NewLine;
new_script += "\r\n" + ReplaceThis(sFile, ctrl, evt, code, "this", "el") + "\r\n" + "}" + "\r\n";
//remove for and event attributes
scriptNode.Attributes["for"].Remove();
scriptNode.Attributes["event"].Remove();
//remove depraciated "language" attribute
//and replace it with "type" attribute
if (scriptNode.Attributes["language"] != null)
scriptNode.Attributes["language"].Remove();
if (scriptNode.Attributes["type"] == null)
scriptNode.Attributes.Add("type", "text/" + lang);
//replace old javascript with a function code
//HERE new_script variable contains the correct value but when I check scriptNode.InnerHtml after assignment, it shows the messed up code.
scriptNode.InnerHtml = new_script;
這是非常奇怪的,我似乎無法找到解決方案。
我已經採用的HTMLEncode
試圖scriptNode.InnerHtml = HtmlDocument.HtmlEncode(new_script);
和產生正確的腳本,在第二個例子說明,但<
和>
全部換成了<
和>
等
所以結果是:
<script type="text/JScript">
function thisForm_onsubmit(el)
{
var Checked = false
var Counter = 0
for (;Counter < el.choice.length; Counter++)
{
if (el.choice[Counter].checked)
{
Checked = true
el.action = el.choice[Counter].value
}
}
if (!Checked)
{
alert ("Please make a selection")
return false
}
}
</script>
我想到的使用InnerText而不是InnerHtml,這更有意義,因爲我正在改變的不是真正的HTML,而是InnerText屬性是隻讀的。
任何人都可以闡明爲什麼會發生這種情況,如果有解決方法嗎?
你多線程工作,並在那裏,這是獲得共享的東西在這些線程之間?它看起來像是當多個線程都更新相同的值時會發生什麼 - 您從一個輸出的一部分輸出與另一個輸出的一部分輸出混合。 –
不,沒有多線程 – ElenaDBA
這是非常奇怪的,因爲到目前爲止我已經轉換爲許多文件的JavaScript沒有問題,只是這個特定的一個給我一個問題 – ElenaDBA