0

のすべてのインスタンスを変更したい

http[s]://www.mydomain.com/test/index.php?this_page=foo

https://www.mydomain.com/test/index.php?this_page=bar

できるはずだと思いますがattr.replace、表記がわかりません。jQueryまたはJavaScriptでこれを行うにはどうすればよいですか?

編集:これまでのところ、誰もが最初の部分(httpまたはhttpsがhttpsに変更)または2番目の部分(fooからbar)が欠落しているようです:)混乱して申し訳ありません

4

7 に答える 7

3

あなたがすることができます:

$('a[href$="://www.mydomain.com/test/index.php?this_page=foo"]')
    .attr('href', 'https://www.mydomain.com/test/index.php?this_page=bar');

attribute-ends-withセレクター[docs]はネイティブの CSS3 セレクターであるため、これらの要素の選択は非常に高速です。もちろん、http[s]and などの部分に正規表現を使用することもできますが.filter、これは遅くなる可能性があります (リンクの数にもよります)。

URL に、新しい URL の一部である必要がある他のパラメーターが含まれている場合は、各リンクを個別に処理する必要があります。

// Using the attribute-contains selector now. This still assumes that 
// `this_page=foo` is the first parameter in the URL
var pattern = /^https?:\/\/www.mydomain.com/test/index.php?this_page=foo(&|$)/;
$('a[href*="://www.mydomain.com/test/index.php?this_page=foo"]')
  .attr('href', function(i, val) {
      return val.replace(pattern, 'https://www.mydomain.com/test/index.php?this_page=bar$1');
  });

URL の任意の場所にある場合は、ベース URLthis_pageを含むすべてのリンクを取得できます。this_page=fooこれは、で始まる他のページ値がない場合にのみ機能しますfoo

var pattern_page = /([?&])this_page=foo(&|$)/;

$('a[href*="://www.mydomain.com/test/index.php?"][href*="this_page=foo"]')
  .attr('href', function(i, val) {
      return val.replace('http:', 'https:')
                .replace(pattern_page, '$1this_page=bar$2');
  });
于 2012-06-22T20:43:35.657 に答える
1

一般的な構文は次のようになります。

$('a').prop('href', function(index, value) {
    return newValue;
});

の計算はnewValue、URL をどのように変換するかによって異なります。つまり、次のようになります。

$('a').prop('href', function(index, value) {
    if (value.match(/=foo\??/) {
        value = value.replace(/=foo\??/, '=bar?').replace(/^http:/, 'https:');
    }
    return value;
});       

完全修飾 URL にアクセスしたいように思われるためで.prop()はなく、使用しました。.attr()属性ではなくプロパティを使用すると、属性に URL スキームが常に含まれるとは限りません。

于 2012-06-22T20:43:25.553 に答える
0

プレーンな JavaScript では、次を使用できます。

var links = document.links;

for (var i = 0, len = links.length; i < len; i++) {
    href = links[i].href;
    if (href.indexOf('https') == -1) {
        links[i].href = href.replace('http', 'https');
        href = links[i].href;
    }
    if (href.split('=')[1] == 'foo'){
        links[i].href = href.replace('foo','bar');
    }
}​

JS フィドルのデモ

上記では、2 つの置換が必ずしも相互に依存しているとは限らないという (おそらく間違った) 仮定の下で作業しました。もしそうなら、それはより簡単になります:

var links = document.links;

for (var i = 0, len = links.length; i < len; i++) {
    href = links[i].href;
    if (href.indexOf('https') == -1 && href.split('=')[1] == 'foo') {
        links[i].href = href.replace('http', 'https');
        links[i].href = href.replace('foo','bar');
    }
}​

JS フィドルのデモ

于 2012-06-22T20:51:07.580 に答える
0

すべてのインスタンスを置き換えます!!

$("a").each(function() {
    if ($(this).attr("href") == 'http[s]://www.mydomain.com/test/index.php?this_page=foo') {
        $(this).attr('href', 'https://www.mydomain.com/test/index.php?this_page=foo');
    }
});​
于 2012-06-22T20:41:56.697 に答える
0

hrefこれがリンクの属性に含まれていると仮定して、次のことを試してください。

$("a").each(function() {
    if($(this).attr("href").indexOf("http:") != -1) {
        $(this).attr("href", $(this).attr("href").replace("http", "https"));
    }
});

もっと効率的な方法があるかもしれませんが、これでうまくいくはずです。

よろしく、リチャード

于 2012-06-22T20:41:57.650 に答える
0

これにより、アンカー内のすべての http 参照がページ全体で https に置き換えられます。

​$.each($('a'), function(i,v){
  var oldULR =  $(v).attr('href');
  var newURL = oldULR.replace('http', 'https');
  $(this).attr('href', newURL);

})​;​

フィドル: http://jsfiddle.net/bkNLD/

于 2012-06-22T20:42:07.273 に答える
0

これを見つけた他の人にとっては、私と同じくらい初心者でした...これが私が最終的に使用したソリューションです。

$(document).ready(function(){ 
  $('a[href*="this_page=foo"]').prop('href', function(index, value) {
  return value.replace(/=foo/, '=bar').replace(/^http:/, 'https:');
  });
});

これは Felix Kling と Alnitak の組み合わせであり、両氏に感謝します。

于 2012-06-22T23:50:49.207 に答える