1

タッチデバイスの場合、ホバーせずに直接クリックするとどのように言えますか?
再生する例は次のとおりです: http://jsfiddle.net/2Sax4/4/

ポイントは、a href を取得するための言い方です。このようなもの:

window.location= 'http://' + this a; 

jQuery:

$(function(){

    // not touch:      
    $(".menu li").hover(function() {
        $(this).animate({ "background-color":red }, 1000);
    },function() {
        $(this).animate({ "background-color":blue }, 1000);
    });

    // touch:
    var supportsTouch = 'ontouchstart' in window || 'onmsgesturechange' in window;
    if(supportsTouch) {
        $(this).on('touchstart', function(){
                    //I tried something like this
            window.location= 'http://' + this a;  
        }); 

    }   
})

HTML:

<ul class="menu">
    <li id="first"><a href="first.html">first</a></li>
    <li id="second"><a href="second.html">second</a></li>
</ul>
4

4 に答える 4

3
window.location.href = location.protocol + '//' + location.host + '/' + $( 'a', this).prop('href');

プロトコルまたはホスト (ホスト名 + ポート) がわからない場合。

于 2013-04-29T08:16:20.503 に答える
2

これを試して

window.location= $("a", this).attr("href");

もちろん、あなたのコードから私が考える をthis参照すれば、これは機能します。li

于 2013-04-29T08:14:33.447 に答える
1

この場合の $(this) は何ですか? 一般に、次のことができます。

window.location.href = $(this).attr('href');$(this) が<a>.

于 2013-04-29T08:13:55.353 に答える
0

次の 2 つの手順があります。

  1. 要素への参照を取得しaます。
  2. に適切な URL を割り当てますwindow.location.href

リンクへの参照を取得するには、要素の子孫でそれを見つける必要があります。

var link = $(this).find('a'); // or $(this).children('a');

現在、hrefHTML 属性には URL の一部 (ファイル名) しか含まれていませんが、対応するDOM プロパティには、ページの現在の URL に基づく完全なURL (スキーム、ホスト、パスなど) が含まれています。
したがって、それを取得してに割り当てることができますwindow.location/href

window.location.href = link.prop('href');
于 2013-04-29T09:11:31.343 に答える