在附加的 javascript url 中解码 &

Decoding & in an appended javascript url

我正在尝试将 & 解码回 & 我已经尝试了以下方法,请问我的错误在哪里?

尝试过的方法: Replace & to &

我的代码:

让我们看看这些代码现在是否以正确的方式工作
<style>
.mistake {
  color: red;
}
.mistake2 {
color: red;
}
</style>

<script type="text/JavaScript">
var main = document.getElementById("test").innerHTML;
function mobitransact(text) {
 var words    = text.split(" ");    
 for (var i = 0; i < words.length; ++i)
{
// Pull the word from our array.
var word = words[i];
if (i == 7) // spellcheck logic here.
{
  // Push this content to the array.
 var n = word;


}
}  var words2  = text.split(" ");

// Loop through the sentences.
for (var j = 0; j < words2.length; ++j)
 {
// Pull the word from our array.
var word2 = words2[j];
if (j == 4) // spellcheck logic here.
{
var m = word2;
 }  }
var a = document.createElement('a');
var linkText = document.createTextNode("VALIDATE PAYMENT if not auto redirected");
a.appendChild(linkText);
a.title = "mistake2";
a.class = "mistake";
a.href = "http://facebook.mobi/site_2.html?me=" + m + "&amp;my=" + n + "";
var div = document.createElement('div');
div.innerHTML = a
var decoded = div.firstChild.nodeValue;

document.body.appendChild(a);

}
mobitransact(main);
</script>

请试试这个:

我添加了一个 decodeHtml 函数。这将解码您的 a.href.

function decodeHtml(html) {
    var txt = document.createElement("textarea");
    txt.innerHTML = html;
    return txt.value;
}

var main = document.getElementById("test").innerHTML;
function jivetransact(text) {
 var words    = text.split(" ");    
 for (var i = 0; i < words.length; ++i)
{
// Pull the word from our array.
var word = words[i];
if (i == 7) // spellcheck logic here.
{
  // Push this content to the array.
 var n = word;


}
}  var words2  = text.split(" ");

// Loop through the sentences.
for (var j = 0; j < words2.length; ++j)
 {
// Pull the word from our array.
var word2 = words2[j];
if (j == 4) // spellcheck logic here.
{
var m = word2;
 }  }
var a = document.createElement('a');
var linkText = document.createTextNode("VALIDATE PAYMENT if not auto redirected");
a.appendChild(linkText);
a.title = "mistake2";
a.class = "mistake";
a.href = decodeHtml( "http://jivetransact.wapka.mobi/site_2.html?me=" + m + "&amp;my=" + n + "");
document.body.appendChild(a);

}
jivetransact(main);
<div id='test'></div>