0

ページの読み込み時にタッチ イベントを呼び出す方法がわかりません。以下のコードで試しましたが、イベントが発生しません。誰が私がどこで間違っているのか教えてもらえますか?

$(window).onload = setTimeout(function () {
    document.getElementById('resetBtn').style.display = 'none';
    $('#splash').attr('src', 'img/splash.png');
    disableZoomButtons();
    setTimeout(function () {
        $("#splashDiv").remove();
        $('#menuBg').attr('src', 'img/wood.png');
        $('#about').attr('src', 'img/about1.png');
        $('#help').attr('src', 'img/help1.png');
        $('#newGame').attr('src', 'img/newgame.png');

        $('#newGame').on('touchstart', function (e) {
            alert("Start");
            enableZoomButtons();
            can = document.getElementById('can');
            cxt = can.getContext('2d');
            init();
        });
    });
}, 2000);

ここにHTMLコードがあります

<body>

<img id="background" />
<div id="splashDiv">
    <img id="splash" src="img/splash.png"
        style="width: 100%; height: 100%; z-index: 1; position: absolute;" />
</div>
<div id="menuBgDiv">
    <img id="menuBg" src="img/wood.png"
        style="width: 100%; height: 100%; z-index: -1; position: fixed;" />
<ul>
    <li><img class="menus" id="newGame" src="img/newgame.png" />
    </li>
    <li><img class="menus" id="help" src="img/help1.png" />
    </li>
    <li><img class="menus" id="about" src="img/about1.png" />
    </li>
</ul>
</div>
</body>

onload 関数でタッチイベントを呼び出すことは可能ですか (トリガーではありません)

newGame id は、HTML で定義されたイメージ ID です。newGame 画像ボタンをクリックしてから、init() メソッドを呼び出す必要があります。

4

2 に答える 2

1

これを試してイベントをトリガーしてください:

$('#newGame').on('touchstart', function (e) {
    alert("Start");
    enableZoomButtons();
    can = document.getElementById('can');
    cxt = can.getContext('2d');
    init();
})
.trigger("touchstart");
于 2013-11-12T13:09:14.150 に答える
0

それ以外の

$(window).onload

使用する

$(document).ready( function() {} );

考えられる問題は、実際の要素をロードする前でもイベントを添付していることです。また、setTimeout の用途は何ですか? よくわかりません。上記のコードを使用して、setTimeout を削除してください。

$(document).ready( function() {
    document.getElementById('resetBtn').style.display = 'none';
    setTimeout(function () {
        $('#splash').attr('src', 'img/splash.png');
        $("#splashDiv").remove();
        $('#menuBg').attr('src', 'img/wood.png');
        $('#about').attr('src', 'img/about1.png');
        $('#help').attr('src', 'img/help1.png');
        $('#newGame').attr('src', 'img/newgame.png');
        $('#newGame').click( function (e) {
            alert("Start");
            can = document.getElementById('can');
            cxt = can.getContext('2d');
        });
    }, 2000);
});

編集済み:不要な機能を削除しましたが、再度配置できます

于 2013-11-12T13:13:40.533 に答える