Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/419.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
编辑javascript文档。写入(变量)_Javascript - Fatal编程技术网

编辑javascript文档。写入(变量)

编辑javascript文档。写入(变量),javascript,Javascript,我有一个JS函数 function setComparison(data) { var w= window.open('', 'comparison', 'width=600, height=400'); w.document.open(); w.document.write(getComparisonContent(data)); w.document.close(); return false; } 我无法访问创建(数据)的代码,但我需要替换(数据)

我有一个JS函数

function setComparison(data) {
    var w= window.open('', 'comparison', 'width=600, height=400');
    w.document.open();
    w.document.write(getComparisonContent(data));
    w.document.close();
    return false;
}
我无法访问创建(数据)的代码,但我需要替换(数据)中的字符串。有没有一种方法可以通过字符串替换方法传递(数据)?

似乎是您最好的选择

function setComparison(data) {
    var w= window.open('', 'comparison', 'width=600, height=400');
    w.document.open();
    data = data.replace("foo", "bar");
    w.document.write(getComparisonContent(data));
    w.document.close();
    return false;
}

这里的
foo
将替换为
bar

前面的答案对我不起作用,但它确实起了作用:

function setComparison(data) {
    var w= window.open('', 'comparison', 'width=600, height=400');
    w.document.open();
    html = html.replace("string to replace", "replacement text");
    w.document.write(getComparisonContent(data));
    w.document.close();
    return false;
}

Regular Expressions
是你的朋友。“数据”中到底是什么?如果
data
是字符串,你可以使用
data.replace()
。你能做什么?您可以更改
setComparison(data)
函数的内部代码吗?使用
innerHTML
而不是
doc。编写
什么是
html
?你的问题在哪里?这太荒谬了。