0

我正在尝试替换元素的 html 代码。它看起来像这样:

<a class="disp_img" onclick="increase('\x6c\x6f\x61\x64\x5f\x6d\x65\x6e\x75\x28\x24\x28\x27\x23\x69\x6d\x61\x67\x65\x73\x27\x29\x2e\x68\x74\x6d\x6c\x28\x29\x2c\x20\x24\x28\x27\x23\x69\x6d\x61\x67\x65\x73\x5f\x32\x27\x29\x2e\x68\x74\x6d\x6c\x28\x29\x2c\x20\x27\x72\x61\x6e\x64\x6f\x6d\x27\x2c\x20\x27\x73\x74\x72\x69\x63\x74\x27\x29');" href="javascript:void(0);">Display Images</a>

这是:

 'load_menu($('#images').html(), $('#images_2').html(), 'random', 'strict')'

但它需要保持十六进制。

我只想用“严格”替换“随机”,如下所示:

var found = [];
var changed = [];
var c = 0;
var searchText = "Display Images";

var as = document.getElementsByClassName("disp_img");
for (var i = 0; i < as.length; i++) {
    if (as[i].innerText == searchText){
    found[c] = as[i].outerHTML;
        c++;
    }
}

for (i = 0; i < found.length; i++) {
    changed[i] = found[i].toString();
    changed[i] = changed[i].replace("\x27\x72\x61\x6e\x64\x6f\x6d\x27\x2c\x20\x27\x73\x74\x72\x69\x63\x74\x27", "\x27\x73\x74\x72\x69\x63\x74\x27\x2c\x20\x27\x73\x74\x72\x69\x63\x74\x27");
    console.log(changed[i]);
}

我可以管理其余的,但我遇到了这个问题。如何replace正确使用方法来替换该字符串?

4

1 回答 1

0

好,我懂了。我需要双反斜杠,比如.replace("\\x27\\x72\\x61等。

于 2013-08-03T16:25:36.637 回答