0

私が使用する場合:

jQuery(document).ready(function() {

      console.log($(this));

});

次に、コンソールにオブジェクトがあります:

[Document mypage.html#weather]

この最後の ID を取得するにはどうすればよいですか? この例では#weatherです。たとえば、セレクターでアラート#weatherを使用したい$(data_from_console + '.add').val();

4

2 に答える 2

3

ID 属性を持つ最後の要素を取得しますか?

もしそうなら:

$(document).ready(function () {
    console.log($('body *[id]').eq(-1));
});

EDIT よく見ると、ハッシュタグを探していますか? すなわち。URL がmypage.html#weather#weather が必要な場合は ?

その場合は、次を試してください。

$(document).ready(function () {
    console.log(document.location.hash);
});
于 2012-08-14T12:18:41.263 に答える
2

last()メソッドを使用できます。これを試して:

$(document).ready(function() {
    $('body *').each(function() { //selects all elements in body which got id
      var lastEle = $(this).last();   //selects last one of them
      console.log(lastEle.attr('id'));//returns it's id to console log
    });
});
于 2012-08-14T12:12:57.107 に答える