2

理解しようとしているRegExに問題があります。

たとえば、私はURLを持っています

http://localhost:8888/faqs/#answer290

私がやりたいのは、URL をチェックして、URL にハッシュが含まれているかどうかを確認し、含まれている場合はその前のすべてを削除することです。

#answer290

私がこれまでに持っているのは

$("div#questions ul li a").click(function(e){
    var selected = $(this).attr('href');
    console.log(selected);
    if(window.location.href.indexOf("#") > -1) {
        var location = window.location,
            result = /\/([^\/]*)$/.exec(location)[1];
        selected = result;
        console.log(result);
        alert("your url contains #");
    } else {
        selected = selected
    }
    $('.top-button').remove();
    $('.current-faq').removeClass();
    $.scrollTo(selected, 400);
    $(selected).append('<a href="#" class="top-button">TOP</a>');
    $(selected).addClass('current-faq');
});

私がやっていることの詳細をFAQページで説明すると、ハッシュとスクロールで対応するセクションが見つかりますが、問題は、別のページから来たとき、選択したURLが実際のFAQページにあるときとは異なることです.

FAQ ページではアラートが発生しますが、何らかの理由で、質問にリンクしている別のページに移動するとアラートが発生しません。

URL全体を別のページから削除しようとしているので、ハッシュの後にすべてが含まれています。

私は近いようですが、最後の部分を取得するのに苦労しています

ありがとう

4

2 に答える 2

1

そのために正規表現は必要ありません。indexOf/substring が行います:

var s = "http://localhost:8888/faqs/#answer290";

var i = s.indexOf("#")

if (i != -1) {
    alert(s.substring(i, s.length));
}

アンカーのハッシュ プロパティを読み取る必要がある場合は、 を使用できます.prop('hash')。例えば

<a id="link" href="http://localhost:8888/faqs/#answer290">link</a>

$("#link").prop("hash")「#answer290」を取得します

于 2013-09-26T18:31:40.603 に答える
0

何かのようなもの

  var index = window.location.indexOf("#");
  var location = window.location;
  if(index != -1) {
     location = location.substring(index, location.length);
  }
于 2013-09-26T18:33:53.967 に答える