0

JavaScript で解決するのが難しい問題がもう 1 つあります。以下に示すように、文字列変数はフォーム入力値の外側から取得されます

document.addEventListener('DOMContentLoaded', function() {
var Country = document.getElementById('countr').value;
}, false);

変更する必要がある文字列アドレスは、相対 href アドレスの一部であり、2 つの異なる形式を取ります。

モデル/世界/some_site.html

また

models/local/some_site.html

変数 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

しかし、それは機能しません。理由はわかりません。どんな助けも私にとって非常に貴重です。

国変数が処理されたくないことがわかりました。なぜですか? このスクリプトは私の変数をまったく表示しません。

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
}
4

2 に答える 2

1

models/local/some_site.htmlテキストの中央部分を国変数に置き換えたいとします。

関数を使用split()して、に基づいて文字列を分割できます/。分割された文字列を取得した後、index[1]国の値が含まれているlocalworld、置換されるため、値を取得できます。
そして、replace()関数を使用してテキストを置き換えることができます。

以下のサンプルコード:

    <script language="javascript">
    var address="models/local/some_site.html";
    var Country="france";//Example
    var finaladdress=address.replace(address.split("/")[1],Country);    
    </script>

完全なソリューション:

コード 1:Country変数を次のように宣言します。global

var Country;//declare the Country outside the function so it becomes global
document.addEventListener('DOMContentLoaded', function() {
Country = document.getElementById('countr').value;
}, false);

コード 2:replacementここで行う

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(adres.split("/")[1]),document.getElementById('countr').value); // here I need replacement
}
于 2013-11-11T08:40:20.367 に答える
1

まず、join()は配列に使用され、結果の文字列にカンマが残るため、concat()代わりに使用する必要があります。第二に、次のreplace()ように、はるかに単純なソリューションになります。

var regex = /(world|local)/;
address.replace(regex,Country);

編集:

コードをテストしたので、メソッド自体は機能します。その場合の問題は、Country変数にある必要があります。そのスコープがグローバルであると確信していますか? あなたの関数の表示方法は、イベントリスナーの外部のどこにも表示されないことを示唆しています。または、グローバルが悪いと考える場合は、イベント リスナーをコードの残りの部分に直接リンクし、それを関数の引数として渡すことができます。

于 2013-11-11T08:49:18.500 に答える