我还有另一个难题需要用 JavaScript 解决。字符串变量来自外部表单输入值,如下所示
document.addEventListener('DOMContentLoaded', function() {
var Country = document.getElementById('countr').value;
}, false);
我需要更改的字符串地址是相对 href 地址的一部分,将采用两种不同的形式。
模型/世界/some_site.html
或者
模型/本地/some_site.html
变量 Country 也会发生变化,例如:German、England、France 等。
如果我想用 Country 变量替换“世界”或“本地”,我需要做的就是做类似的事情
var address = address.split('world').join(Country).split('local').join(Country);
或者
var address = address.replace('world', new RegExp(Country, 'g')).replace('local', new RegExp(Country, 'g'));
结果应该是这样的
模型/德语/some_site.html
但它不起作用我不知道为什么。任何帮助对我来说都是非常宝贵的。
我发现我的 Country 变量不想被处理,为什么?该脚本根本不显示我的变量。
document.addEventListener('DOMContentLoaded', function() {
var Country = document.getElementById('cotr').value;
}, false);
document.write(Country);
所以这个例子也不能正常工作
address.replace(/(local|world)/,Country)
有什么帮助吗?
我的真实代码如下所示
Indeks.prototype.wyswietl = function(adres)
{
if (typeof adres == 'undefined') adres =
document.forms[this.id].elements['hasla'].value;
if (typeof this.ramka == 'string')
{
var okno =
window.open(adres, this.ramka, 'menubar=yes,toolbar=yes,location=yes,directories=no,status=yes,scrollbars=yes,resizable=yes');
if (okno) okno.focus();
else window.location.href = adres;
}
else this.ramka.location.href =
adres.replace(/(world|local)/,Country);//here I need replacement adres is address
}